Golang: 接收GET和POST參數(shù)
GET 和 POST 是我們最常用的兩種請(qǐng)求方式,今天講一講如何在 golang 服務(wù)中,正確接收這兩種請(qǐng)求的參數(shù)信息。
處理GET請(qǐng)求
1.1 接收GET請(qǐng)求
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
|
//接收GET請(qǐng)求 func Get(writer http.ResponseWriter , request *http.Request) { query := request.URL.Query() // 第一種方式 // id := query["id"][0] // 第二種方式 id := query.Get("id") log.Printf("GET: id=%s\n", id) fmt.Fprintf(writer, `{"code":0}`) } func main(){ http.HandleFunc("/get", Get) log.Println("Running at port 9999 ...") err := http.ListenAndServe(":9999", nil) if err != nil { log.Fatal("ListenAndServe: ", err.Error()) } } |
Postman 發(fā)起get請(qǐng)求
重新運(yùn)行程序,請(qǐng)求Postman,服務(wù)端控制臺(tái)打印如下:
2020/12/04 11:33:55 Running at port 9999 ...
2020/12/04 11:34:09 GET: id=123
1.2 接收GET請(qǐng)求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
func Get(writer http.ResponseWriter , request *http.Request) { result := make(map[string]string) keys := request.URL.Query() for k, v := range keys { result[k] = v[0] } log.Println(result) } func main(){ http.HandleFunc("/get", Get) log.Println("Running at port 9999 ...") err := http.ListenAndServe(":9999", nil) if err != nil { log.Fatal("ListenAndServe: ", err.Error()) } } |
重新運(yùn)行程序,請(qǐng)求Postman,服務(wù)端控制臺(tái)打印如下:
2020/12/04 12:37:17 Running at port 9999 ...
2020/12/04 12:37:21 map[id:123 name:sina]
需要注意的是,這里的req.URL.Query()返回的是數(shù)組,因?yàn)間o可以接收id=1&id=2這樣形式的參數(shù)并放到同一個(gè)key下
接收POST請(qǐng)求
在開(kāi)發(fā)中,常用的 POST 請(qǐng)求有兩種,分別是 application/json 和 application/x-www-form-urlencoded,下面就來(lái)介紹一下這兩種類(lèi)型的處理方式。
1.1 接收application/x-www-form-urlencoded類(lèi)型的POST請(qǐng)求
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
|
func handlePostForm(writer http.ResponseWriter, request *http.Request) { request.ParseForm() // 第一種方式 // username := request.Form["username"][0] // password := request.Form["password"][0] // 第二種方式 username := request.Form.Get("username") password := request.Form.Get("password") fmt.Printf("POST form-urlencoded: username=%s, password=%s\n", username, password) fmt.Fprintf(writer, `{"code":0}`) } func main(){ http.HandleFunc("/handlePostForm", handlePostForm) log.Println("Running at port 9999 ...") err := http.ListenAndServe(":9999", nil) if err != nil { log.Fatal("ListenAndServe: ", err.Error()) } } |
Postman 發(fā)起x-www-form-urlencoded請(qǐng)求
重新運(yùn)行程序,請(qǐng)求Postman,服務(wù)端控制臺(tái)打印如下:
2020/12/04 12:44:32 Running at port 9999 ...
POST form-urlencoded: username=李四, password=12
1.2 接收application/x-www-form-urlencoded類(lèi)型的POST請(qǐng)求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
func PostForm(w http.ResponseWriter, r *http.Request) { var result = make(map[string]string) r.ParseForm() for k,v := range r.PostForm { if len(v) < 1 { continue } result[k] = v[0] } log.Println(result) } func main(){ http.HandleFunc("/PostForm", PostForm) log.Println("Running at port 9999 ...") err := http.ListenAndServe(":9999", nil) if err != nil { log.Fatal("ListenAndServe: ", err.Error()) } } |
重新運(yùn)行程序,請(qǐng)求Postman,服務(wù)端控制臺(tái)打印如下:
2020/12/04 12:49:40 Running at port 9999 ...
2020/12/04 12:49:45 map[password:12 username:李四]
2 處理 application/json 請(qǐng)求
實(shí)際開(kāi)發(fā)中,往往是一些數(shù)據(jù)對(duì)象,我們需要將這些數(shù)據(jù)對(duì)象以 JSON 的形式返回,下面我們就來(lái)添加一段代碼:
JSON 結(jié)構(gòu)
比如,請(qǐng)求了手機(jī)歸屬地的接口,json 數(shù)據(jù)返回如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
{ "resultcode": "200", "reason": "Return Successd!", "result": { "province": "浙江", "city": "杭州", "areacode": "0571", "zip": "310000", "company": "中國(guó)移動(dòng)", "card": "" } } |
思路是這樣的:
1.先將 json 轉(zhuǎn)成 struct。
2.然后 json.Unmarshal() 即可。
json 轉(zhuǎn) struct ,自己手寫(xiě)就太麻煩了,有很多在線(xiàn)的工具可以直接用,我用的這個(gè):
https://mholt.github.io/json-to-go/
在左邊貼上 json 后面就生成 struct 了。
用代碼實(shí)現(xiàn)下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//AutoGenerated 結(jié)構(gòu)體 type AutoGenerated struct { Resultcode string `json:"resultcode"` Reason string `json:"reason"` Result struct { Province string `json:"province"` City string `json:"city"` Areacode string `json:"areacode"` Zip string `json:"zip"` Company string `json:"company"` Card string `json:"card"` } `json:"result"` } |
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
|
func PostJson(w http.ResponseWriter, r *http.Request ) { body ,err := ioutil.ReadAll(r.Body) if err != nil { log.Println( err) } log.Printf("%s",body) var data AutoGenerated json.Unmarshal([]byte(body),&data) //Json結(jié)構(gòu)返回 json,_ := json.Marshal(data) w.Write(json) } func main(){ http.HandleFunc("/PostJson", PostJson) log.Println("Running at port 9999 ...") err := http.ListenAndServe(":9999", nil) if err != nil { log.Fatal("ListenAndServe: ", err.Error()) } } |
Postman 發(fā)起application/json 請(qǐng)求
重新運(yùn)行程序,訪(fǎng)問(wèn)頁(yè)面,服務(wù)端控制臺(tái)打印如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
2020/12/04 12:57:01 { "resultcode": "200", "reason": "Return Successd!", "a":"dd", "result": { "province": "浙江", "city": "杭州", "areacode": "0571", "zip": "310000", "company": "中國(guó)移動(dòng)", "card": "33" } } |
到此這篇關(guān)于GO接收GET/POST參數(shù)及發(fā)送GET/POST請(qǐng)求的文章就介紹到這了,更多相關(guān)GO接收GET/POST參數(shù)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_27312939/article/details/110632297