文档
- https://pkg.go.dev/net/http
- https://pkg.go.dev/net/url
目录
- 1、发起GET请求
- 2、发起带参GET请求
- 3、POST请求提交Form表单
- 4、POST请求提交Json数据
- 5、接收响应数据,json转为map
- 6、自定义请求头
1、发起GET请求
使用net/http
可以很容易发起get请求
package mainimport ("fmt""io""net/http"
)func main() {resp, _ := http.Get("https://httpbin.org/get")defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Print(string(body))
}
响应
{"args": {}, "headers": {"Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "Go-http-client/2.0", "X-Amzn-Trace-Id": "Root=1-664863e9-34028ecc4c56c08d6ac5d923"}, "origin": "127.0.0.1", "url": "https://httpbin.org/get"
}
2、发起带参GET请求
使用net/url
将查询参数拼接到url上,再使用net/http
发起http请求
package mainimport ("fmt""io""net/http""net/url"
)func main() {targetUrl := "https://httpbin.org/get"u, _ := url.ParseRequestURI(targetUrl)// URL paramdata := url.Values{}data.Set("name", "Tom")data.Set("age", "18")u.RawQuery = data.Encode() // URL encodefmt.Println(u.String())// https://httpbin.org/get?age=18&name=Tomresp, _ := http.Get(u.String())defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))}
响应
{"args": {"age": "18", "name": "Tom"}, "headers": {"Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "Go-http-client/2.0", "X-Amzn-Trace-Id": "Root=1-6648641d-0567278a093ee36078e9da27"}, "origin": "127.0.0.1", "url": "https://httpbin.org/get?age=18&name=Tom"
}
3、POST请求提交Form表单
package mainimport ("fmt""io""net/http""net/url"
)func main() {targetUrl := "https://httpbin.org/post"data := url.Values{}data.Set("name", "Tom")data.Set("age", "18")resp, _ := http.PostForm(targetUrl, data)defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))
}
响应
{"args": {}, "data": "", "files": {}, "form": {"age": "18", "name": "Tom"}, "headers": {"Accept-Encoding": "gzip", "Content-Length": "15", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Go-http-client/2.0", "X-Amzn-Trace-Id": "Root=1-66486446-48a3d6085e91c39208746892"}, "json": null, "origin": "127.0.0.1", "url": "https://httpbin.org/post"
}
4、POST请求提交Json数据
package mainimport ("bytes""encoding/json""fmt""io""net/http"
)func main() {targetUrl := "https://httpbin.org/post"data := make(map[string]interface{})data["name"] = "Tom"data["age"] = 12respdata, _ := json.Marshal(data)resp, _ := http.Post(targetUrl, "application/json", bytes.NewReader(respdata))defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))}
响应
{"args": {}, "data": "{\"age\":12,\"name\":\"Tom\"}", "files": {}, "form": {}, "headers": {"Accept-Encoding": "gzip", "Content-Length": "23", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "Go-http-client/2.0", "X-Amzn-Trace-Id": "Root=1-6648646b-1057cc454774771009b19914"}, "json": {"age": 12, "name": "Tom"}, "origin": "127.0.0.1", "url": "https://httpbin.org/post"
}
5、接收响应数据,json转为map
package mainimport ("encoding/json""fmt""io""net/http"
)// 定义响应数据结构
type Result struct {Args string `json:"args"`Headers map[string]string `json:"headers"`Origin string `json:"origin"`Url string `json:"url"`
}func main() {targetUrl := "https://httpbin.org/get"resp, _ := http.Get(targetUrl)defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))// 接收返回的数据var ret Resultjson.Unmarshal(body, &ret)fmt.Printf("%#v\n", ret)}
响应数据json格式
{"args": {}, "headers": {"Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "Go-http-client/2.0", "X-Amzn-Trace-Id": "Root=1-664854df-48710d487d9f9d97398586ac"}, "origin": "127.0.0.1", "url": "https://httpbin.org/get"
}
响应数据golang数据
main.Result{Args:"", Headers:map[string]string{"Accept-Encoding":"gzip", "Host":"httpbin.org", "User-Agent":"Go-http-client/2.0", "X-Amzn-Trace-Id":"Root=1-664854df-48710d487d9f9d97398586ac"}, Origin:"127.0.0.1", Url:"https://httpbin.org/get"
}
6、自定义请求头
通过构建 Request 对象,设置请求头属性
package mainimport ("fmt""io""net/http"
)func main() {targetUrl := "http://httpbin.org/get"client := &http.Client{}req, _ := http.NewRequest("GET", targetUrl, nil)req.Header.Add("X-Token", "123456")req.Header.Add("X-UID", "666")resp, _ := client.Do(req)defer resp.Body.Close()body, _ := io.ReadAll(resp.Body)fmt.Println(string(body))
}
响应
{"args": {}, "headers": {"Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "Go-http-client/1.1", "X-Amzn-Trace-Id": "Root=1-664863a4-044bd06c4e4887d73790591a", "X-Token": "123456", "X-Uid": "666"}, "origin": "127.0.0.1""url": "http://httpbin.org/get"
}
参考: Go实现 简单http请求(get ,post) 多种请求方式