https://github.com/chyroc/requests
https://github.com/chyroc/requests
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/chyroc/requests
- Owner: chyroc
- License: apache-2.0
- Created: 2023-12-01T09:33:25.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-19T07:58:29.000Z (over 1 year ago)
- Last Synced: 2025-01-29T08:23:15.380Z (12 months ago)
- Language: Go
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# requests
## Install
```shell
go get github.com/chyroc/requests
```
## Usage
sample usage
```go
package main
import (
"fmt"
"net/http"
"time"
"github.com/chyroc/requests"
)
func Example_Method() {
r := requests.Get("https://httpbin.org/get")
r = requests.Post("https://httpbin.org/post")
r = requests.Delete("https://httpbin.org/delete")
r = requests.New(http.MethodPut, "https://httpbin.org/put")
fmt.Println(r.Text().Unpack())
}
func Example_unmarshal() {
r := requests.Post("https://httpbin.org/post")
type Response struct {
// ...
}
resp := requests.JSON[Response](r)
fmt.Println(resp.Unpack())
}
func Example_factory() {
// I hope to set fixed parameters every time I initiate a request
// Then, every request created by this factory will not log
opt := requests.Options(
requests.WithLogger(requests.DiscardLogger()),
requests.WithTimeout(time.Second*10),
requests.WithQuery("query", "value"),
requests.WithHeader("Auth", "hey"),
)
// Send sample request
r := requests.Get("https://httpbin.org/get", opt...)
r = requests.Post("https://httpbin.org/get").
WithLogger(requests.DiscardLogger()).
WithTimeout(time.Second * 10)
fmt.Println(r.Text().Unpack())
}
func Example_newSession() {
session := requests.NewSession("/tmp/requests-session.txt")
r := session.Get("https://jsonplaceholder.typicode.com/todos/1").
WithTimeout(time.Second * 10)
fmt.Println(r.Text().Unpack())
}
```