Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ido50/requests
High-level, API-centric HTTP client for Go
https://github.com/ido50/requests
api api-client go golang http https requests rest rest-api restful
Last synced: 14 days ago
JSON representation
High-level, API-centric HTTP client for Go
- Host: GitHub
- URL: https://github.com/ido50/requests
- Owner: ido50
- License: apache-2.0
- Created: 2018-05-27T10:00:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-31T12:53:24.000Z (4 months ago)
- Last Synced: 2024-10-24T00:53:23.271Z (23 days ago)
- Topics: api, api-client, go, golang, http, https, requests, rest, rest-api, restful
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 8
- Watchers: 2
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
requests
high-level HTTP client for Go.
---
`requests` is a high-level, API-centric HTTP client for Go projects. It is meant
to provide a more comfortable mechanism to perform requests to HTTP APIs (rather
than making general requests), and to prevent common mistakes made when using
`net/http` directly.With `requests`, one must not need to remember to read HTTP responses in full (so
Go can reuse TCP connections), nor to close response bodies. Handling of JSON
data - be it in requests or responses - is made easier by way of built-in
encoders/decoders. An automatic retry mechanism is also included.The library allows a "DRY" (Dont Repeat Yourself) approach to REST API usage by
introducing API-specific dependencies into the client object. For example,
authorization headers and response handlers can be set in the client object,
and all generated requests will automatically include them.# Install
```
go get -u github.com/ido50/requests
```# Usage
```go
package mainimport (
"fmt"
"net/http"
"time""github.com/ido50/requests"
)const apiURL = "https://my.api.com/v2"
type RequestBody struct {
Title string `json:"title"`
Tags []string `json:"tags"`
Publish bool `json:"publish"`
}type ResponseBody struct {
ID int64 `json:"id"`
Date time.Time `json:"date"`
}func main() {
client := requests.
NewClient(apiURL).
Accept("application/json").
BasicAuth("user", "pass").
RetryLimit(3)var res ResponseBody
err := client.
NewRequest("POST", "/articles").
JSONBody(RequestBody{
Title: "Test Title",
Tags: []string{"test", "stories"},
Publish: true,
}).
ExpectedStatus(http.StatusCreated).
Into(&res).
Run()
if err != nil {
panic(err)
}fmt.Printf("Created article %d on %s\n", res.ID, res.Date.Format(time.RFC3339))
}
```