https://github.com/hr3lxphr6j/requests
A "Requests" style HTTP client for golang
https://github.com/hr3lxphr6j/requests
golang http http-client requests
Last synced: 11 months ago
JSON representation
A "Requests" style HTTP client for golang
- Host: GitHub
- URL: https://github.com/hr3lxphr6j/requests
- Owner: hr3lxphr6j
- License: mit
- Created: 2020-07-06T13:27:35.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-29T06:04:58.000Z (almost 5 years ago)
- Last Synced: 2025-02-26T18:57:03.273Z (12 months ago)
- Topics: golang, http, http-client, requests
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Requests
[](https://travis-ci.org/hr3lxphr6j/requests)
[](https://goreportcard.com/report/github.com/hr3lxphr6j/requests)
[](https://codecov.io/gh/hr3lxphr6j/requests)
A "Requests" style HTTP client for golang
## Install
```shell
go get -u github.com/hr3lxphr6j/requests
```
## Example
```go
var (
timeout = time.Second * 3
url = "http://example.com"
queryParams = map[string]string{"foo": "bar"}
body = map[string]interface{}{"a": "b", "nums": []int{1, 2, 3}}
)
// Do `curl --connect-timeout 3 -d '{"a": "b", "num": [1, 2, 3]}' -H 'Content-Type: application/json' http://example.com?foo=bar`
// With net/http
func UseNetHttp() map[string]interface{} {
ctx, _ := context.WithTimeout(context.Background(), timeout)
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
log.Panic(err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
if err != nil {
log.Panic(err)
}
req.Header.Set("Content-Type", "application/json")
value := urlPkg.Values{}
for k, v := range queryParams {
value.Set(k, v)
}
req.URL.RawQuery = value.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
data := map[string]interface{}{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
log.Panic(err)
}
return data
}
// With Requests
func UseRequests() map[string]interface{} {
resp, err := requests.Post(url,
requests.Timeout(timeout),
requests.Queries(queryParams),
requests.JSON(body),
)
if err != nil {
log.Panic(err)
}
data := map[string]interface{}{}
if err := resp.JSON(&data); err != nil {
log.Panic(err)
}
return data
}
```