https://github.com/fako1024/httpc
A simple wrapper around the default Go http client optimized for ease-of-use
https://github.com/fako1024/httpc
http-client method-chaining openapi-specification openapi3
Last synced: 8 months ago
JSON representation
A simple wrapper around the default Go http client optimized for ease-of-use
- Host: GitHub
- URL: https://github.com/fako1024/httpc
- Owner: fako1024
- License: apache-2.0
- Created: 2019-09-16T12:42:06.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-04-24T14:37:47.000Z (10 months ago)
- Last Synced: 2025-06-28T20:58:39.264Z (8 months ago)
- Topics: http-client, method-chaining, openapi-specification, openapi3
- Language: Go
- Homepage:
- Size: 184 KB
- Stars: 3
- Watchers: 4
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# A simple wrapper around the default Go http client optimized for ease-of-use
[](https://github.com/fako1024/httpc/releases)
[](https://godoc.org/github.com/fako1024/httpc/)
[](https://goreportcard.com/report/github.com/fako1024/httpc)
[](https://github.com/fako1024/httpc/actions?query=workflow%3AGo)
[](https://github.com/fako1024/httpc/actions/workflows/codeql-analysis.yml)
This package wraps the Go standard http client, providing a simplified interaction model using method chaining and additional capabilities such as optional in-flow validation against an OpenAPI specification.
## Features
- Simple, method chaining based interface for HTTP client requests
- Simulation of request delays
- Validation of request + response against OpenAPI specification
- Customization of HTTP client via functional parameter
- Back-Off-Retry concept to automatically retry requests if required
## Installation
```bash
go get -u github.com/fako1024/httpc
```
## Examples
#### Perform simple HTTP GET request
```go
err := httpc.New("GET", "http://example.org").Run()
if err != nil {
log.Fatalf("error performing GET request: %s", err)
}
```
#### Perform HTTP GET request and parse the result as JSON into a struct
```go
var res = struct {
Status int
Message string
}{}
err := httpc.New("GET", "http://example.org").
ParseJSON(&res).
Run()
if err != nil {
log.Fatalf("error performing GET request: %s", err)
}
```
#### Perform HTTPS POST request with a simple body, disabling certificate validation and copying the response to a bytes.Buffer
```go
buf := new(bytes.Buffer)
err := httpc.New("POST", "https://example.org").
SkipCertificateVerification().
Body([]byte{0x1, 0x2}).
ParseFn(httpc.Copy(buf)).
Run()
if err != nil {
log.Fatalf("error performing POST request: %s", err)
}
fmt.Println(buf.String())
```
#### Perform HTTPS GET request (with query parameters + headers + basic auth), validating request and response against OpenAPIv3 specification
```go
openAPIFileData, err := os.ReadFile("/tmp/openapi.json")
if err != nil {
log.Fatalf("Error opening OpenAPI specification file: %s", err)
}
err = httpc.New("GET", "https://example.org").
SkipCertificateVerification().
QueryParams(httpc.Params{
"param": "test",
}).
Headers(httpc.Params{
"X-HEADER-TEST": "test",
}).
AuthBasic("username", "password").
OpenAPIValidationFileData(openAPIFileData).
Run()
if err != nil {
log.Fatalf("error performing GET request: %s", err)
}
```