requests

所属分类:WEB开发
开发工具:GO
文件大小:38KB
下载次数:0
上传日期:2022-12-08 15:13:44
上 传 者sh-1993
说明:  Go的高级、以API为中心的HTTP客户端
(High-level, API-centric HTTP client for Go)

文件列表:
.golangci.yml (962, 2022-12-08)
LICENSE (11357, 2022-12-08)
go.mod (211, 2022-12-08)
go.sum (46617, 2022-12-08)
requests.go (36859, 2022-12-08)
requests_test.go (15069, 2022-12-08)

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 main import ( "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 int*** `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)) } ```

近期下载者

相关文件


收藏者