在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ddliu/go-httpclient开源软件地址:https://github.com/ddliu/go-httpclient开源编程语言:Go 100.0%开源软件介绍:go-httpclientAdvanced HTTP client for golang. Features
Installationgo get github.com/ddliu/go-httpclient Quick Startpackage main
import (
"github.com/ddliu/go-httpclient"
)
func main() {
httpclient.Defaults(httpclient.Map {
httpclient.OPT_USERAGENT: "my awsome httpclient",
"Accept-Language": "en-us",
})
res, err := httpclient.Get("http://google.com/search", map[string]string{
"q": "news",
})
println(res.StatusCode, err)
} UsageSetupUse httpclient.Defaults(httpclient.Map {
httpclient.OPT_USERAGENT: "my awsome httpclient",
"Accept-Language": "en-us",
}) The Sending Request// get
httpclient.Get("http://httpbin.org/get", map[string]string{
"q": "news",
})
// get with url.Values
httpclient.Get("http://httpbin.org/get", url.Values{
"q": []string{"news", "today"}
})
// post
httpclient.Post("http://httpbin.org/post", map[string]string {
"name": "value"
})
// post file(multipart)
httpclient.Post("http://httpbin.org/multipart", map[string]string {
"@file": "/tmp/hello.pdf",
})
// put json
httpclient.PutJson("http://httpbin.org/put",
`{
"name": "hello",
}`)
// delete
httpclient.Delete("http://httpbin.org/delete")
// options
httpclient.Options("http://httpbin.org")
// head
httpclient.Head("http://httpbin.org/get") Customize RequestBefore you start a new HTTP request with httpclient.
WithHeader("User-Agent", "Super Robot").
WithHeader("custom-header", "value").
WithHeaders(map[string]string {
"another-header": "another-value",
"and-another-header": "another-value",
}).
WithOption(httpclient.OPT_TIMEOUT, 60).
WithCookie(&http.Cookie{
Name: "uid",
Value: "123",
}).
Get("http://github.com") ResponseThe // traditional
res, err := httpclient.Get("http://google.com")
bodyBytes, err := ioutil.ReadAll(res.Body)
res.Body.Close()
// ToString
res, err = httpclient.Get("http://google.com")
bodyString, err := res.ToString()
// ReadAll
res, err = httpclient.Get("http://google.com")
bodyBytes, err := res.ReadAll() Handle Cookiesurl := "http://github.com"
httpclient.
WithCookie(&http.Cookie{
Name: "uid",
Value: "123",
}).
Get(url)
for _, cookie := range httpclient.Cookies() {
fmt.Println(cookie.Name, cookie.Value)
}
for k, v := range httpclient.CookieValues() {
fmt.Println(k, v)
}
fmt.Println(httpclient.CookieValue("uid")) Concurrent SafeIf you want to start many requests concurrently, remember to call the go func() {
httpclient.
Begin().
WithHeader("Req-A", "a").
Get("http://google.com")
}()
go func() {
httpclient.
Begin().
WithHeader("Req-B", "b").
Get("http://google.com")
}() Error CheckingYou can use res, err := httpclient.Get("http://google.com")
if httpclient.IsTimeoutError(err) {
// do something
} Full ExampleSee OptionsAvailable options as below:
Seperate ClientsBy using the If you need more than one client in a single programme. Just create and use them seperately. c1 := httpclient.NewHttpClient().Defaults(httpclient.Map {
httpclient.OPT_USERAGENT: "browser1",
})
c1.Get("http://google.com/")
c2 := httpclient.NewHttpClient().Defaults(httpclient.Map {
httpclient.OPT_USERAGENT: "browser2",
})
c2.Get("http://google.com/") |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论