An Example of Go Language Development Sending Get and Post Requests

Time:2019-6-9

When developing in the Go language, sometimes you may have to send get or post requests. Here’s a brief introduction to post and get requests: about HTTP protocol

HTTP (Hypertext Transfer Protocol) is one of the most common and commonly used protocols in modern networks. Its purpose is to ensure the communication between client and server.

HTTP works in a “request-response” protocol between the client and the server.

The client can be a Web browser and the server can be some network applications on the computer.

Usually, the browser initiates HTTP requests to the server, and the server returns the response to the browser. The response contains the status information of the request and the content that may be requested.

When requesting web pages in Go language, it is implemented with net/http package. Officials have provided detailed explanations, but in a rough way, I have made some additions myself.

In general, there are several ways to request web pages:

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

GET request:

The GET request splices the parameters directly into the 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 Plaza in Tianhe District of Guangzhou & region=Guangzhou&city_limit=true&output=json&ak=yX8nC9Qzpckek7lY9g GWmlD4TFcA2Yx3")
 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 kinds of post-related requests: http.post, http.postForm and http.Do requests.

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 the 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 http. do to make requests


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))
}

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.

Recommended Today

[Redis5 source code learning] analysis of the randomkey part of redis command

baiyan Command syntax Command meaning: randomly return a key from the currently selected databaseCommand format: RANDOMKEY Command actual combat: 127.0.0.1:6379> keys * 1) “kkk” 2) “key1” 127.0.0.1:6379> randomkey “key1” 127.0.0.1:6379> randomkey “kkk” Return value: random key; nil if database is empty Source code analysis Main process The processing function corresponding to the keys command is […]