Go-http-client: An enhanced http client for Golang

go-http-client

go-http-client

An enhanced http client for Golang

Documentation on go.dev 🔗

Coverage awesome

This package provides you a http client package for your http requests. You can send requests quicly with this package. If you want to contribute this package, please fork and create a pull request.

Installation

$ go get -u github.com/bozd4g/go-http-client/

Usage

import (
	"encoding/json"
	"fmt"
	client "github.com/bozd4g/go-http-client"
)

type Todo struct {
    Id        int
    UserId    int
    Title     string
    Completed bool
}

func main() {
    httpClient := client.New("https://jsonplaceholder.typicode.com/")
    request, err := httpClient.Get("posts/10")
    
    if err != nil {
        panic(err)
    }
    
    response, err := httpClient.Do(request)
    if err != nil {
        panic(err)
    }
    
    var todo Todo
    err = json.Unmarshal(response.Get().Body, &todo)
    if err != nil {
        panic(err)
    }
    fmt.Println(todo.Title) // Lorem ipsum dolor sit amet

    // or  
    var todo2 Todo     
    response, err = httpClient.Do(request)
    if err == nil {
        response.To(&todo2)
        fmt.Println(todo2.Title) // Lorem ipsum dolor sit amet
    } else {
        fmt.Println(err.Error())
    }
}

Functions

You can call these functions from your application.

Function Has Params
Get(endpoint string) -
GetWith(endpoint string, params interface {}) Yes
Post(endpoint string) -
PostWith(endpoint string, params interface {}) Yes
Patch(endpoint string) -
PatchWith(endpoint string, params interface{}) Yes
Put(endpoint string) -
PutWith(endpoint string, params interface{}) Yes
Delete(endpoint string) -
DeleteWith(endpoint string, params interface{}) Yes
Do() (Response, error) -
To(value interface{}) -

License

Copyright (c) 2020 Furkan Bozdag

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Owner
Furkan Bozdag
no pointers to point to anything.
Furkan Bozdag
Similar Resources

A golang client for the Twitch v3 API - public APIs only (for now)

go-twitch Test CLIENT_ID="my client ID" go test -v -cover Usage Example File: package main import ( "log" "os" "github.com/knspriggs/go-twi

Sep 27, 2022

a Go (Golang) MusicBrainz WS2 client library - work in progress

a Go (Golang) MusicBrainz WS2 client library - work in progress

gomusicbrainz a Go (Golang) MusicBrainz WS2 client library - a work in progress. Current state Currently GoMusicBrainz provides methods to perform sea

Sep 28, 2022

Golang client for LastPass

Go client for LastPass Features login via user name and master password two-factor authentication with out-of-band mechanism such as push notification

Dec 23, 2022

Golang Mixpanel Client

mixpanel Mixpanel Go Client Donate to 1P8ccYhVt4ByLahuVXiCY6U185gmYA8Rqf Usage import "github.com/dukex/mixpanel" -- documentation on godoc Examples T

Dec 16, 2022

Golang client for PayPal REST API

Go client for PayPal REST API Coverage POST /v1/oauth2/token POST /v1/identity/openidconnect/tokenservice GET /v1/identity/openidconnect/userinfo/?sch

Dec 25, 2022

An API client for the Notion API implemented in Golang

An API client for the Notion API implemented in Golang

Dec 30, 2022

The client of NodeStatus / Golang Version

NodeStatus-client-go The client of NodeStatus written in Golang 使用说明 请直接下载release下的对应平台的二进制文件。 运行时需传入客户端对应参数。 假设你的服务端地址是https://tz.mydomain.com,客户端用户名

Dec 26, 2022

This is the GoLang ApiDQ API client

ApiDQ API GoLang Client This is the GoLang ApiDQ API client. This library allows using of the actual API version. You can find more info in the docume

Oct 19, 2021

golang client for illumina's basespace

a REST API client communicate with Illumina's BaseSpace The easist way to get a corresponding token https://developer.basespace.illumina.com/docs/cont

Jan 10, 2022
Comments
  • Large response payload cannot be fully read due to context cancelled

    Large response payload cannot be fully read due to context cancelled

    Hi,

    I've noticed that for some larger payloads in the response (in my case it was just an array of 40 objects or so) I was getting context cancelled error when reading the response body. After digging a bit more into the code and reading the docs of net/http I' found that it was caused by use of timed context within your sendReq method.

    reqCtx, cancel := context.WithTimeout(ctx, c.timeout)
    defer cancel()
    

    Following the docs of NewRequestWithContext:

    For an outgoing client request, the context controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body.

    So what happens here is that once a request is made, you return Response object that might be still buffering response payload. However, cancel() happens around the same time, which, in case of larger payloads that are not fully buffered before sendReq's defer is called, results in partial response body and error returned when calling response.Body()

    One thing would be to pass ctxCancel function to the response object returned and call cancel as part of reading the response payload - however, docs of net/http.Client says:

    // Timeout specifies a time limit for requests made by this // Client. The timeout includes connection time, any // redirects, and reading the response body. The timer remains // running after Get, Head, Post, or Do return and will // interrupt reading of the Response.Body. suggest that timeout set when the client is created does most likely exactly the same what your reqCtx, cancel := context.WithTimeout(ctx, c.timeout) does. So keeping it in sendReq is unnecessary and one can rely on the internal implementation of the client.timeout

A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Kinesumer is a Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Jan 5, 2023
Clusterpedia-client - clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Jan 7, 2022
Client-go - Clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Dec 5, 2022
⚡️ SharePoint authentication, HTTP client & fluent API wrapper for Go (Golang)
⚡️ SharePoint authentication, HTTP client & fluent API wrapper for Go (Golang)

Gosip - SharePoint authentication, HTTP client & fluent API wrapper for Go (Golang) Main features Unattended authentication using different strategies

Jan 2, 2023
Go client for wit.ai HTTP API
Go client for wit.ai HTTP API

This repository is community-maintained. We gladly accept pull requests. Please see the Wit HTTP Reference for all supported endpoints. Go client for

Jan 7, 2023
Client for the cloud-iso-client

cloud-iso-client Client for the cloud-iso-client. Register an API token Before using this client library, you need to register an API token under your

Dec 6, 2021
Nutanix-client-go - Go client for the Nutanix Prism V3 API

nutanix-client-go This repository contains portions of the Nutanix API client code in nutanix/terraform-provider-nutanix. It has been extracted to red

Jan 6, 2022
Aoe4-client - Client library for aoe4 leaderboards etc

AOE4 Client Overview This is a go client used to query AOE4 data from either the

Jan 18, 2022
Balabola-go-client - GO client for Yandex balabola service

Balabola GO Client GO client for Yandex balabola service Yandex warning The neur

Jan 29, 2022
Golang client for ethereum json rpc api

Ethrpc Golang client for ethereum JSON RPC API. web3_clientVersion web3_sha3 net_version net_peerCount net_listening eth_protocolVersion eth_syncing e

Jan 7, 2023