https://github.com/dillonstreator/request
golang http request library
https://github.com/dillonstreator/request
http http-client json-api
Last synced: 8 months ago
JSON representation
golang http request library
- Host: GitHub
- URL: https://github.com/dillonstreator/request
- Owner: dillonstreator
- License: mit
- Created: 2022-09-21T20:50:27.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-10T21:11:21.000Z (over 3 years ago)
- Last Synced: 2025-05-22T02:11:40.878Z (9 months ago)
- Topics: http, http-client, json-api
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# request
go request library for http apis
## Installation
```bash
go get github.com/dillonstreator/request
```
## Usage
```go
client := request.NewClient("https://jsonplaceholder.typicode.com/todos")
todos := []struct {
ID int `json:"id"`
UserID int `json:"userId"`
Title string `json:"title"`
Completed bool `json:"completed"`
}{}
values := url.Values{}
values.Add("userId", "2")
headers := http.Header{}
res, err := client.Get(context.Background(), "/", headers, values, &todos)
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
fmt.Println(todos)
```
### Custom http client
```go
httpClient := &http.Client{
Timeout: time.Second * 5,
}
client := request.NewClient(
"https://jsonplaceholder.typicode.com/todos",
request.WithHTTPClient(httpClient),
)
```
### Custom error handling
```go
type CustomError struct {
Detail string `json:"detail"`
}
func (c *CustomError) Error() string {
return fmt.Sprintf("custom error details: %s", c.Detail)
}
client := request.NewClient(
"https://jsonplaceholder.typicode.com/todos",
request.WithErrChecker(func(req *http.Request, res *http.Response) error {
if res.StatusCode != http.StatusOK { // your custom error handling here...
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
custErr := &CustomError{}
err = json.Unmarshal(b, custErr)
if err != nil {
return err
}
return custErr
}
return nil
}),
)
items := []struct {
// ...
}{}
_, err := client.Get(context.Background(), "/", nil, nil, &items)
if err != nil {
if custErr, ok := err.(*CustomError); ok {
log.Fatal(custErr.Detail)
}
}
fmt.Println(items)
```
### Token auth
```go
client := request.NewClient(
"https://some-token-authed-api.com",
request.WithTokenAuth("Bearer "),
)
client = request.NewClient(
"https://some-token-authed-api.com",
request.WithTokenAuth("Token "),
)
client = request.NewClient(
"https://some-token-authed-api.com",
request.WithTokenAuth(""),
)
```
### Basic auth
```go
client := request.NewClient(
"https://some-basic-authed-api.com",
request.WithBasicAuth("user", "pass"),
)
```
### All together
```go
customHTTPClient := &http.Client{
Timeout: time.Second * 5,
}
client := request.NewClient(
"https://some-token-authed-api.com",
request.WithHTTPClient(customHTTPClient),
request.WithTokenAuth("Bearer "),
request.WithErrChecker(func(req *http.Request, res *http.Response) error {
if res.StatusCode != http.StatusOK {
return fmt.Errorf("some error occurred %d %s%s", res.StatusCode, req.URL.Host, req.URL.Path)
}
return nil
}),
)
```