-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse_test.go
63 lines (56 loc) · 1.41 KB
/
response_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package go_requests
import (
"encoding/json"
"fmt"
"testing"
)
func TestParseJsonResponse(t *testing.T) {
r := Request{
Method: "get",
Url: "https://httpbin.org/get?name=张三&age=12"}
resp := r.Send()
respJson := resp.Json()
//fmt.Println(resp.Text)
args := respJson["args"].(map[string]interface{})
name := args["name"].(string)
age := args["age"].(string)
fmt.Println(name, age)
}
func TestParseJsonResponseToStruct(t *testing.T) {
type Args struct {
Name string `json:"name"`
Age string `json:"age"`
}
type Headers struct {
Accept_Encoding string `json:"Accept-Encoding"`
Host string `json:"Host"`
User_Agent string `json:"User-Agent"`
X_Amzn_Trace_Id string `json:"X-Amzn-Trace-Id"`
}
type MyResponse struct {
Args Args `json:"args"`
Headers Headers `json:"headers"`
Origin string `json:"origin"`
Url string `json:"url"`
}
r := Request{
Method: "get",
Url: "https://httpbin.org/get?name=张三&age=12"}
resp := r.Send()
fmt.Println(resp.Text)
//
var respObj MyResponse
err := json.Unmarshal(resp.Content, &respObj)
if err != nil {
fmt.Println("JSON反序列化失败")
}
name := respObj.Args.Name
age := respObj.Args.Age
fmt.Println(name, age)
}
func TestResponseGet(t *testing.T) {
resp := Get("https://httpbin.org/get?name=张三&age=12", nil)
name := resp.Get("args.name")
age := resp.Get("args.age")
fmt.Println(name, age)
}