Table of Contents
- HTTP request methods
- Sample request
- GET
- POST
- Kv form
- json
- PUT
- PATCH
- DELETE
- Process the response
- Source reference
- Complete the test code
HTTP request methods
According to the HTTP standard, HTTP requests can use multiple request methods.
There are five request methods that most people use in daily development: GET, POST, PUT, PATCH, and DELETE
methods | describe |
---|---|
GET | Requests the specified page information and returns the entity body. |
POST | Submit data to a specified resource for processing requests (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources. |
PUT | Data transferred from the client to the server replaces the contents of the specified document. |
DELETE | Asks the server to delete the specified page. |
PATCH | Is a complement to the PUT method and is used to perform local updates on known resources. |
Sample request
GET
HTTP.Get direct access
import (
"net/http"
)
response, err := http.Get("https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1")
Copy the code
This approach is suitable for methods that do not require headers
Custom parameter access
import (
"net/http"
)
url := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization"."xxxx")
response, err := http.DefaultClient.Do(req)
Copy the code
This approach is suitable for scenarios where you need to customize some headers
When you look at the HTTP.Get method source code, you can see that it is an easy way to use
func (c *Client) Get(url string) (resp *Response, err error) {
req, err := NewRequest("GET", url, nil)
iferr ! =nil {
return nil, err
}
return c.Do(req)
}
Copy the code
POST
In POST mode commonly used is 2,
- Transmission via KV, for example
form-data
和x-www-form-urlencoded
- Send it as json, for example
application/json
Kv form
import (
"net/http"
"strings"
)
url := "https://blog.csdn.net/zyndev"
payload := strings.NewReader("a=111")
response, err := http.Post(url, "application/x-www-form-urlencoded", payload)
Copy the code
In addition to http.Post, you can also use http.PostForm
import (
"net/http"
"net/url"
)
targetUrl := "https://blog.csdn.net/zyndev"
payload := url.Values{"key": {"value"}, "id": {"123"}}
response, err := http.PostForm(targetUrl, payload)
Copy the code
json
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("POST", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
Copy the code
PUT
Since NET/HTTP does not provide a simplified PUT request, http.newRequest is used to create the request
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("PUT", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
Copy the code
PATCH
Since NET/HTTP does not provide a simplified PATCH request, http.newRequest is used to create the request
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("PATCH", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
Copy the code
DELETE
Since NET/HTTP does not provide a simplified DELETE request, http.newRequest is used to create the request
targetUrl := "https://ddbc5ffb-c596-4f78-a99d-a6ea93bdc14f.mock.pstmn.io/user/1"
req, _ := http.NewRequest("DELETE", targetUrl, nil)
req.Header.Add("Authorization"."xxxx")
response, err := http.DefaultClient.Do(req)
Copy the code
Process the response
As can be seen from the above example, no matter what kind of request is used, response and ERR will be obtained at last, that is, the processing logic is the same no matter what way the request is initiated.
.// A bunch of request method constructs
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
// Error logic handling
}
defer response.Body.Close() // This step is necessary to prevent future memory leaks, remember
fmt.Println(response.StatusCode) // Get the status code
fmt.Println(response.Status) // Get the copy corresponding to the status code
fmt.Println(response.Header) // Get the response header
body, _ := ioutil.ReadAll(response.Body) // Read the response body, which returns []byte
fmt.Println(string(body)) // Convert to a string to see the result
Copy the code
Source reference
Complete the test code
package http_demo
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
)
func TestHttpGet(t *testing.T) {
response, err := http.Get("https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1")
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
fmt.Println(response.StatusCode)
fmt.Println(response.Status)
fmt.Println(response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}
func TestHttpGetHeader(t *testing.T) {
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
req, _ := http.NewRequest("GET", targetUrl, nil)
req.Header.Add("Authorization"."xxxx")
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpPost(t *testing.T) {
targetUrl := "https://blog.csdn.net/zyndev"
payload := strings.NewReader("a=111")
response, err := http.Post(targetUrl, "x-www-form-urlencoded", payload)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpPostForm(t *testing.T) {
targetUrl := "https://blog.csdn.net/zyndev"
payload := url.Values{"key": {"value"}, "id": {"123"}}
response, err := http.PostForm(targetUrl, payload)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpPostJSON(t *testing.T) {
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("POST", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpPut(t *testing.T) {
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("PUT", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpPatch(t *testing.T) {
targetUrl := "https://b959e645-00ae-4bc3-8a55-7224d08b1d91.mock.pstmn.io/user/1"
payload := strings.NewReader("{\"name\ :\" Zhang Yu - nan \"}")
req, _ := http.NewRequest("PATCH", targetUrl, payload)
req.Header.Add("Content-Type"."application/json")
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
func TestHttpDelete(t *testing.T) {
targetUrl := "https://ddbc5ffb-c596-4f78-a99d-a6ea93bdc14f.mock.pstmn.io/user/1"
req, _ := http.NewRequest("DELETE", targetUrl, nil)
req.Header.Add("Authorization"."xxxx")
response, err := http.DefaultClient.Do(req)
iferr ! =nil {
t.Error(err)
panic(err)
}
defer response.Body.Close()
t.Log(response)
}
Copy the code