package httputil import ( "encoding/json" "fmt" "os" "strconv" "testing" ) type ResponseInfo struct { Gzipped bool `json:"gzipped"` Method string `json:"method"` Origin string `json:"origin"` Useragent string `json:"user-agent"` // http://httpbin.org/user-agent Form map[string]string `json:"form"` Files map[string]string `json:"files"` Headers map[string]string `json:"headers"` Cookies map[string]string `json:"cookies"` } var info ResponseInfo type Resport struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } func Test_Get_Httpcli(t *testing.T) { client := Client() var headers = map[string]string{"auth_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVjZTJiNzNmZTI3Y2YyMmE4ZjQyMTVkMCIsInVzZXJOYW1lIjoidHVib2xpbmcxNjgiLCJpc0xvZ2luIjp0cnVlLCJsb2dpblRpbWUiOjE1NjQxNDYwNzI5MDUsImlhdCI6MTU2NDE0NjA3Mn0.VQICBk71KDITus83pZEq3C1y_WbKPZc0HUfmIm_eby8"} respo, err := client.Get("https://no5.xiexiaojia.com/appjmall/api/showCore/appShowList", headers) defer respo.Body.Body.Close() if err != nil { fmt.Printf("%+v", err) } resport := Resport{} body, err := respo.Body.ReadAll() //反序列化 err = json.Unmarshal(body, &resport) fmt.Println("response", resport) //案例获取数据 fmt.Println("response comName:", resport.Data.([]interface{})[0].(map[string]interface{})["comName"]) } func Test_Post_Httpcli(t *testing.T) { client := Client() fmt.Println("http 客户端", client) var headers = map[string]string{"auth_token": "xxxxx"} var params = map[string]string{"userId": "5ce2b73fe27cf22a8f4215d0", "price": "5000", "count": "1", "commId": "5ce40e03e27cf22a8f4215d8", "comName": "日本ceruru.b 脐带血面膜保湿补水修复5片", "comImageCar": "http://oam.szbslhb.com/4,01fb06b4adf1.jpg", "attributeCar": "", } respo, err := client.Post("https://no5.xiexiaojia.com/appjmall/api/cart/addCommToCart", headers, params) defer respo.Body.Body.Close() if err != nil { fmt.Printf("%+v", err) } resport := Resport{} body, err := respo.Body.ReadAll() if err != nil { res, _ := respo.Body.ToString() fmt.Println("response", res) return } //反序列化 err = json.Unmarshal(body, &resport) fmt.Println("response", resport) } func Test_PostJson_Httpcli(t *testing.T) { client := Client() var headers = map[string]string{"Content-Type": "application/json"} fmt.Println("http 客户端", client) respo, err := client.Get("https://www.kancloud.cn/cattong/go_command_tutorial/261347", headers) defer respo.Body.Body.Close() if err != nil { fmt.Printf("%+v", err) } body, err := respo.Body.ToString() fmt.Println("response", body) } func Test_PutFile_Httpcli(t *testing.T) { client := Client() file, err := os.Open("/Users/mac/Downloads/1567069549ABBQCTFC.mp4") info, _ := file.Stat() var headers = map[string]string{ "Content-Type": "application/octet-stream", "Authorization": "bucketName=bucket-01;accessKey=FSd58ed59fccc16812903c37068de8969b;ttl=1h", "Content-Disposition": "fileName=test02.mp4;size=" + strconv.FormatInt(info.Size(), 10), "Signature": "9cea21bf95f235e7d9c5a70888cf742cfb9c946ed25aef8b70ce77c953ccbff0", } fmt.Println("http 客户端", client) respo, err := client.Put("http://localhost:8888/fs/api/uploadStream", headers, file) defer respo.Body.Body.Close() if err != nil { fmt.Printf("%+v", err) } body, err := respo.Body.ToString() fmt.Println("response", body) }