An example of using HTTP to initiate get and post requests in go language

Time:2020-1-8

About HTTP protocol

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

HTTP works as a request answer 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 sends an HTTP request to the server, and the server returns a response to the browser. The response contains the status information of the request as well as the content that may be requested.

To request web pages in go language, net / HTTP package is used. The official has provided detailed instructions, but it is rough. I have made some additions myself.

In general, there are several ways to request a web page:

Get, head, post, and postform initiate HTTP (or HTTPS) requests:

resp, err := http.Get("http://example.com/")
...
 
//Parameter details
//1. Target URL of the request 
//2. The resource type (MIME) of the data to be post
//3. Bitstream of data ([] byte form)
resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
...
 
//Parameter details
//1. Target URL of the request 
//2. The submitted parameter value can use URL. Values or strings. Newreader ("key = value & id = 123")
//Note that you can also URL. Value and strings. Newreader and use strings. Newreader (URL. Values {}. Encode())
resp, err := http.PostForm("http://example.com/form",
 url.Values{"key": {"Value"}, "id": {"123"}})

Here is the analysis:

Get request


 resp, err := http.Get("http://example.com/")
 if err != nil {
 // handle error
 }
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 
 if err != nil {
 // handle error
 }
 
 fmt.Println(string(body))

Post request (resource submission, such as image upload)


 resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
 if err != nil {
 // handle error
 }
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 
 if err != nil {
 // handle error
 }
 
 fmt.Println(string(body))

Post form submission


 postValue := url.Values{
 "email": {"[email protected]"},
 "password": {"123456"},
 }
 
 resp, err := http.PostForm("http://example.com/login", postValue)
 if err != nil {
 // handle error
 }
 
 defer resp.Body.Close()
 
 body, err := ioutil.ReadAll(resp.Body)
 
 if err != nil {
 // handle error
 }
 
 fmt.Println(string(body))

Extend post form submission (including header settings)

postValue := url.Values{
 "email": {"[email protected]"},
 "password": {"123456"},
 }
 
 postString := postValue.Encode()
 
 req, err := http.NewRequest("POST","http://example.com/login_ajax", strings.NewReader(postString))
 if err != nil {
 // handle error
 }
 
 //Form method (required)
 req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
 //Ajax mode request
 req.Header.Add("x-requested-with", "XMLHttpRequest")
 
 client := &http.Client{}
 resp, err := client.Do(req)
 if err != nil {
 // handle error
 }
 
 defer resp.Body.Close()
 
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
 // handle error
 }
 
 fmt.Println(string(body))

Compare get and post

The following table compares two HTTP methods: get and post

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.