文档
- /net/http
- /net/url
目录
- 1、发起GET请求
- 2、发起带参GET请求
- 3、POST请求提交Form表单
- 4、POST请求提交Json数据
- 5、接收响应数据,json转为map
- 6、自定义请求头
1、发起GET请求
使用net/http
可以很容易发起get请求
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
resp, _ := http.Get("/get")
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Print(string(body))
}
响应
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "",
"User-Agent": "Go-http-client/2.0",
"X-Amzn-Trace-Id": "Root=1-664863e9-34028ecc4c56c08d6ac5d923"
},
"origin": "127.0.0.1",
"url": "/get"
}
2、发起带参GET请求
使用net/url
将查询参数拼接到url上,再使用net/http
发起http请求
package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
targetUrl := "/get"
u, _ := url.ParseRequestURI(targetUrl)
// URL param
data := url.Values{}
data.Set("name", "Tom")
data.Set("age", "18")
u.RawQuery = data.Encode() // URL encode
fmt.Println(u.String())
// /get?age=18&name=Tom
resp, _ := 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": "",
"User-Agent": "Go-http-client/2.0",
"X-Amzn-Trace-Id": "Root=1-6648641d-0567278a093ee36078e9da27"
},
"origin": "127.0.0.1",
"url": "/get?age=18&name=Tom"
}
3、POST请求提交Form表单
package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
func main() {
targetUrl := "/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": "",
"User-Agent": "Go-http-client/2.0",
"X-Amzn-Trace-Id": "Root=1-66486446-48a3d6085e91c39208746892"
},
"json": null,
"origin": "127.0.0.1",
"url": "/post"
}
4、POST请求提交Json数据
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
targetUrl := "/post"
data := make(map[string]interface{})
data["name"] = "Tom"
data["age"] = 12
respdata, _ := 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": "",
"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": "/post"
}
5、接收响应数据,json转为map
package main
import (
"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 := "/get"
resp, _ := http.Get(targetUrl)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body))
// 接收返回的数据
var ret Result
json.Unmarshal(body, &ret)
fmt.Printf("%#v\n", ret)
}
响应数据json格式
{
"args": {},
"headers": {
"Accept-Encoding": "gzip",
"Host": "",
"User-Agent": "Go-http-client/2.0",
"X-Amzn-Trace-Id": "Root=1-664854df-48710d487d9f9d97398586ac"
},
"origin": "127.0.0.1",
"url": "/get"
}
响应数据golang数据
main.Result{
Args:"",
Headers:map[string]string{
"Accept-Encoding":"gzip",
"Host":"",
"User-Agent":"Go-http-client/2.0", "X-Amzn-Trace-Id":"Root=1-664854df-48710d487d9f9d97398586ac"
},
Origin:"127.0.0.1",
Url:"/get"
}
6、自定义请求头
通过构建 Request 对象,设置请求头属性
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
targetUrl := "/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": "",
"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": "/get"
}
参考: Go实现 简单http请求(get ,post) 多种请求方式