Last active
February 26, 2022 14:30
-
-
Save z1076/a84408dfb828a7bf17c4d3e95e5b9938 to your computer and use it in GitHub Desktop.
[Golang网络请求 / resty]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//普通POST请求 | |
resp, err := http.Post( | |
"http://xxxx.com", | |
"application/x-www-form-urlencoded", | |
strings.NewReader(bodyStr)) | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Printf("%+v\n", err) | |
return | |
} | |
respond := string(body) | |
// map 默认content-type为 application/x-www-form-urlencoded。 post form表单请求 | |
param := map[string]string{ | |
"appid": arg.ApiParams["appid"], | |
"out_order_no": result.ChannelOrderSn, | |
} | |
client := resty.New() | |
resp, err := client.R(). | |
SetFormData(param). | |
Post(arg.ApiParams["chargeUrl"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
client := resty.New() | |
//GET | |
resp, err := client.R(). | |
SetQueryParams(map[string]string{ | |
"page_no": "1", | |
"limit": "20", | |
"sort":"name", | |
"order": "asc", | |
"random":strconv.FormatInt(time.Now().Unix(), 10), | |
}). | |
SetHeader("Accept", "application/json"). | |
SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F"). | |
Get("/search_result") | |
resp, err := client.R(). | |
SetQueryString("productId=232&template=fresh-sample&cat=resty&source=google&kw=buy a lot more"). | |
SetHeader("Accept", "application/json"). | |
SetAuthToken("BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F"). | |
Get("/show_product") | |
//获取响应字符串 | |
respond := resp.String() | |
//Post | |
// just mentioning about POST as an example with simple flow | |
// Followed by profile update | |
resp, err := client.R(). | |
SetFormData(map[string]string{ | |
"first_name": "Jeevanandam", | |
"last_name": "M", | |
"zip_code": "00001", | |
"city": "new city update", | |
}). | |
Post("http://myapp.com/profile") | |
// POST JSON string | |
// No need to set content type, if you have client level setting | |
resp, err := client.R(). | |
SetHeader("Content-Type", "application/json"). | |
SetBody(`{"username":"testuser", "password":"testpass"}`). | |
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). | |
Post("https://myapp.com/login") | |
// POST []byte array | |
// No need to set content type, if you have client level setting | |
resp, err := client.R(). | |
SetHeader("Content-Type", "application/json"). | |
SetBody([]byte(`{"username":"testuser", "password":"testpass"}`)). | |
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). | |
Post("https://myapp.com/login") | |
// POST Struct, default is JSON content type. No need to set one | |
resp, err := client.R(). | |
SetBody(User{Username: "testuser", Password: "testpass"}). | |
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). | |
SetError(&AuthError{}). // or SetError(AuthError{}). | |
Post("https://myapp.com/login") | |
// POST Map, default is JSON content type. No need to set one | |
resp, err := client.R(). | |
SetBody(map[string]interface{}{"username": "testuser", "password": "testpass"}). | |
SetResult(&AuthSuccess{}). // or SetResult(AuthSuccess{}). | |
SetError(&AuthError{}). // or SetError(AuthError{}). | |
Post("https://myapp.com/login") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a Resty Client | |
client := resty.New() | |
resp, err := client.R(). | |
EnableTrace(). | |
Get("https://httpbin.org/get") | |
// Explore response object | |
fmt.Println("Response Info:") | |
fmt.Println(" Error :", err) | |
fmt.Println(" Status Code:", resp.StatusCode()) | |
fmt.Println(" Status :", resp.Status()) | |
fmt.Println(" Proto :", resp.Proto()) | |
fmt.Println(" Time :", resp.Time()) | |
fmt.Println(" Received At:", resp.ReceivedAt()) | |
fmt.Println(" Body :\n", resp) | |
fmt.Println() | |
// Explore trace info | |
fmt.Println("Request Trace Info:") | |
ti := resp.Request.TraceInfo() | |
fmt.Println(" DNSLookup :", ti.DNSLookup) | |
fmt.Println(" ConnTime :", ti.ConnTime) | |
fmt.Println(" TCPConnTime :", ti.TCPConnTime) | |
fmt.Println(" TLSHandshake :", ti.TLSHandshake) | |
fmt.Println(" ServerTime :", ti.ServerTime) | |
fmt.Println(" ResponseTime :", ti.ResponseTime) | |
fmt.Println(" TotalTime :", ti.TotalTime) | |
fmt.Println(" IsConnReused :", ti.IsConnReused) | |
fmt.Println(" IsConnWasIdle :", ti.IsConnWasIdle) | |
fmt.Println(" ConnIdleTime :", ti.ConnIdleTime) | |
fmt.Println(" RequestAttempt:", ti.RequestAttempt) | |
fmt.Println(" RemoteAddr :", ti.RemoteAddr.String()) | |
/* Output | |
Response Info: | |
Error : <nil> | |
Status Code: 200 | |
Status : 200 OK | |
Proto : HTTP/2.0 | |
Time : 457.034718ms | |
Received At: 2020-09-14 15:35:29.784681 -0700 PDT m=+0.458137045 | |
Body : | |
{ | |
"args": {}, | |
"headers": { | |
"Accept-Encoding": "gzip", | |
"Host": "httpbin.org", | |
"User-Agent": "go-resty/2.4.0 (https://github.com/go-resty/resty)", | |
"X-Amzn-Trace-Id": "Root=1-5f5ff031-000ff6292204aa6898e4de49" | |
}, | |
"origin": "0.0.0.0", | |
"url": "https://httpbin.org/get" | |
} | |
Request Trace Info: | |
DNSLookup : 4.074657ms | |
ConnTime : 381.709936ms | |
TCPConnTime : 77.428048ms | |
TLSHandshake : 299.623597ms | |
ServerTime : 75.414703ms | |
ResponseTime : 79.337µs | |
TotalTime : 457.034718ms | |
IsConnReused : false | |
IsConnWasIdle : false | |
ConnIdleTime : 0s | |
RequestAttempt: 1 | |
RemoteAddr : 3.221.81.55:443 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment