- Build HTTP request in an easy way.
- The
headers
package provides HTTP header constants. - Shortcut methods for reading string/binary body directly from an URL.
go get -u github.com/cmstar/go-httplib@latest
RequestBuilder
is used to simply build HTTP request.
// Setup RequestBuilder. It supports fluent APIs.
b := httplib.NewBuilder("POST", "http://example.org").
WithQueries(map[string]any{
"q2": "v2",
"q3": 3,
}).
WithHeader("x-custom-value", 112233).
WithForm("f1", "vf1").
WithForm("f2", "vf2")
// Do request. This will send a request like this:
// ====
// POST http://example.org?q2=v2&q3=3 HTTP/1.1
// Host: example.org
// X-Custom-Value: 112233
// <Some other headers such as User-Agent ...>
//
// f1=vf1&f2=vf2
// ====
resp, err := b.ReadString()
// Upload a file.
file, _ := os.Open("/some/file")
defer file.Close()
b = httplib.NewBuilder("PUT", "http://example.org")
b.SetReaderBody(file)
resp := b.MustDo()
Send request directly.
// Send a GET request.
stringResponse, err := httplib.Get("http://example.org")
// Send a POST request with custom headers.
customHeaders := map[string]any{
"Custom-Header": "v1",
"Custom-Header2": "v2",
}
binaryBody := []byte("request-body")
binaryResponse, err := httplib.PostBinaryWithHeaders("http://example.org", binaryBody, customHeaders)