https://github.com/broothie/qst
A Go package for building (and sending) HTTP requests
https://github.com/broothie/qst
go golang http
Last synced: 10 months ago
JSON representation
A Go package for building (and sending) HTTP requests
- Host: GitHub
- URL: https://github.com/broothie/qst
- Owner: broothie
- License: mit
- Created: 2021-12-29T19:34:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-15T21:37:03.000Z (over 2 years ago)
- Last Synced: 2025-03-26T18:52:11.066Z (11 months ago)
- Topics: go, golang, http
- Language: Go
- Homepage:
- Size: 43.9 KB
- Stars: 34
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# qst
[](https://pkg.go.dev/github.com/broothie/qst)
[](https://goreportcard.com/report/github.com/broothie/qst)
[](https://codecov.io/gh/broothie/qst)
[](https://github.com/broothie/qst/actions/workflows/gosec.yml)
`qst` is an `*http.Request` builder. "qst" is short for "quest", which is part of the word "request".
## Installation
```shell script
go get github.com/broothie/qst
```
## Documentation
Detailed documentation can be found at [pkg.go.dev](https://pkg.go.dev/github.com/broothie/qst).
A list of all available options can be found [here](https://pkg.go.dev/github.com/broothie/qst#Option).
## Usage
`qst` uses an options pattern to build `*http.Request` objects:
```go
request, err := qst.NewPatch("https://breakfast.com/api", // New PATCH request
qst.BearerAuth("c0rNfl@k3s"), // Authorization header
qst.Path("/cereals", cerealID), // Query param
qst.BodyJSON(map[string]string{"name": "Life"}), // JSON body
)
```
It can also be used to fire requests:
```go
response, err := qst.Patch("https://breakfast.com/api", // Send PATCH request
qst.BearerAuth("c0rNfl@k3s"), // Authorization header
qst.Path("/cereals", cerealID), // Query param
qst.BodyJSON(map[string]string{"name": "Life"}), // JSON body
)
```
The options pattern makes it easy to define custom options:
```go
func createdSinceYesterday() qst.Option {
yesterday := time.Now().Add(-24 * time.Hour)
return qst.Query("created_at", fmt.Sprintf(">%s", yesterday.Format(time.RFC3339)))
}
func main() {
response, err := qst.Get("https://breakfast.com/api",
qst.BearerAuth("c0rNfl@k3s"),
qst.Path("/cereals"),
createdSinceYesterday(),
)
}
```
### qst.Client
This package also includes a `Client`, which can be outfitted with a set of default options:
```go
client := qst.NewClient(http.DefaultClient,
qst.URL("https://breakfast.com/api"),
qst.BearerAuth("c0rNfl@k3s"),
)
response, err := client.Patch(
// qst.URL("https://breakfast.com/api"), // Not necessary, included via client
// qst.BearerAuth("c0rNfl@k3s"), // Not necessary, included via client
qst.Path("/cereals", cerealID),
qst.BodyJSON(map[string]interface{}{
"name": "Golden Grahams",
}),
)
```
### qst.OptionFunc
`OptionFunc` can be used to add a custom function which is run during request creation:
```go
client := qst.NewClient(http.DefaultClient,
qst.OptionFunc(func(request *http.Request) (*http.Request, error) {
token := dynamicallyGetBearerTokenSomehow()
return qst.BearerAuth(token).Apply(request)
}),
)
```