The Go language develops examples of sending Get and Post requests

  • 2020-06-15 09:18:21
  • OfStack

When developing with Go, it is possible to send get or post requests. Here is a brief introduction to post and get requests: HTTP protocol

HTTP (hypertext Transfer Protocol) is one of the most common and commonly used protocols in modern networks and is designed to ensure communication between clients and servers.

HTTP works as a request-reply protocol between client and server.

The client can be the Web browser, and the server can be some network application on the computer.

Typically, the browser makes an HTTP request to the server, and the server returns a response to the browser. The response contains information about the status of the request and what might be requested.

To request a web page in Go, the net/http package is used. The authorities have provided detailed instructions, but they are rough and I have made some additions myself.

1 There are several ways to request a web page in general:

Get, Head, Post, and PostForm make HTTP (or HTTPS) requests:

GET request:

The GET request concatenates parameters directly into URL, as in the following example:


func GetData() {
 client := &http.Client{}
 resp, err := client.Get("http://api.map.baidu.com/place/v2/suggestion?query= Zhengjia Square, Tianhe District, Guangzhou City &region= Guangzhou &city_limit=true&output=json&ak=yX8nC9Qzpckek7lY9gGWmlD4TFcA2tzYx3")
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(string(body))
}

POST request

There are three types of Post related requests: http. post, ES59en. postForm and http. Do.

http. post request:


func httpPost() {
 resp, err := http.Post("http://www.01happy.com/demo/accept.php",
  "application/x-www-form-urlencoded",
  strings.NewReader("name=cjb"))
 if err != nil {
  fmt.Println(err)
 }
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  // handle error
 }
 
 fmt.Println(string(body))
}

Note: the second parameter in the request must be taken, otherwise an error will be reported.

http.postForm:


func PostData() {
 //client := &http.Client{}
 resp, err := http.PostForm("https://www.pgyer.com/apiv2/app/view", url.Values{"appKey": {"62c99290f0cb2c567cb153c1fba75d867e"},
  "_api_key": {"584f29517115df2034348b0c06b3dc57"}, "buildKey": {"22d4944d06354c8dcfb16c4285d04e41"}})
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  fmt.Println(err)
 }
 fmt.Println(string(body))

}

For more complex http requests, we can use the http.do approach


func httpDo() {
 client := &http.Client{}
 
 req, err := http.NewRequest("POST", "http://www.01happy.com/demo/accept.php", strings.NewReader("name=cjb"))
 if err != nil {
  // handle error
 }
 
 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
 req.Header.Set("Cookie", "name=anny")
 
 resp, err := client.Do(req)
 
 defer resp.Body.Close()
 
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
  // handle error
 }
 
 fmt.Println(string(body))
}

Related articles: