Skip to content
http client with smooth interfaces
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
grpc
.gitignore
.travis.yml
LICENSE
README.md
body.go
client.go
clientClose.go
clientClose111.go
client_test.go
config.go
file.go
go.mod
go.sum
request.go
response.go

README.md

httpclient

Build Status Go Report Card Coverage Status GoDoc

More smooth package for http operations as a client:

  • http request builder
  • http response processor
  • http client extension

http.Request Builder

import "github.com/x-mod/httpclient"

requestBuilder := httpclient.NewRequestBuilder(
        httpclient.URL("https://url"),
        httpclient.Method("GET"),
        httpclient.Query("key", "value"),
        httpclient.Header("key", "value"),
        httpclient.BasicAuth("user", "pass"),
        httpclient.Credential(*tlsconfig),
        httpclient.Body(
            httpclient.JSON(map[string]interface{}{
                "a": "hello",
                "b": true,
                "c": 1,
            }),
        ),
    )

req, err := requestBuilder.Get()

http.Response Processor

//ResponseProcessor interface
type ResponseProcessor interface {
	Process(context.Context, *http.Response) error
}

Implement your own ResponseProcessor

http.Client Extension

extend the http.Client with more useful interfaces:

import "github.com/x-mod/httpclient"

client := httpclient.New(
    httpclient.MaxConnsPerHost(16),
    httpclient.Retry(3),
    httpclient.Response(
        httpclient.NewDumpResponse(),
    ),
)

//get standard http.Client
c := client.GetClient()
//get standard http.Transport
tr := client.GetTransport()

//extension fn
err := client.Execute(context.TODO())
err := client.ExecuteRequest(context.TODO(), request)
You can’t perform that action at this time.