https://github.com/cmstar/go-httplib
A library for HTTP requests.
https://github.com/cmstar/go-httplib
golang http-client http-request
Last synced: about 1 year ago
JSON representation
A library for HTTP requests.
- Host: GitHub
- URL: https://github.com/cmstar/go-httplib
- Owner: cmstar
- License: mit
- Created: 2022-06-20T12:03:38.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-07-05T12:32:33.000Z (almost 3 years ago)
- Last Synced: 2025-02-15T16:49:17.715Z (over 1 year ago)
- Topics: golang, http-client, http-request
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httplib - A library for HTTP requests
[](https://pkg.go.dev/github.com/cmstar/go-httplib)
[](https://github.com/cmstar/go-httplib/actions?query=workflow%3AGo)
[](https://codecov.io/gh/cmstar/go-httplib)
[](https://opensource.org/licenses/MIT)
[](https://github.com/cmstar/go-httplib/blob/main/go.mod)
[](https://goreportcard.com/report/github.com/cmstar/go-httplib)
## Features
- 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.
## Install
```bash
go get -u github.com/cmstar/go-httplib@latest
```
## Quick start
### RequestBuilder
`RequestBuilder` is used to simply build HTTP request.
```go
// 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
//
//
// 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()
```
### Shortcuts
Send request directly.
```go
// 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)
```