Home  >  Article  >  Backend Development  >  Use the http.NewRequest function to create a new GET request object and set the request headers and parameters

Use the http.NewRequest function to create a new GET request object and set the request headers and parameters

王林
王林Original
2023-07-25 22:40:441895browse

Use the http.NewRequest function to create a new GET request object, and set the request headers and parameters

In Go language, we can use the http.NewRequest function to create a new GET request object and set request headers and parameters. http.NewRequestThe function accepts three parameters: request method, request URL and request body. We can use the http.MethodGet constant to represent the GET request method, and use a string to represent the request URL to be sent. If there is no request body, you can pass in nil.

We first need to import the net/http and fmt packages:

import (
    "fmt"
    "net/http"
)

Then, we can use http.NewRequest The function creates a new GET request object and sets the request headers and parameters. The sample code is as follows:

func main() {
    url := "https://example.com/api"
    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        fmt.Println("创建请求失败:", err)
        return
    }

    // 设置请求头
    req.Header.Add("Authorization", "Bearer token123")

    // 设置请求参数
    query := req.URL.Query()
    query.Add("key1", "value1")
    query.Add("key2", "value2")
    req.URL.RawQuery = query.Encode()

    // 发送请求
    client := http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("发送请求失败:", err)
        return
    }
    defer resp.Body.Close()

    // 处理响应
    fmt.Println("状态码:", resp.StatusCode)
}

In the above example, we created a GET request object and set the request headers and parameters. We can use the req.Header.Add method to add the request header, use the req.URL.Query method to get the query parameters of the request URL, and use the query.Add Method adds parameters.

Finally, we use http.Client to send the request and resp.StatusCode to get the status code of the response. Here we use the defer resp.Body.Close() statement to ensure that the response body is closed after use to avoid resource leaks.

Summary: Use the http.NewRequest function to easily create a new GET request object and set the request headers and parameters. We can add request headers and parameters by calling relevant methods. The code for sending requests and processing responses is also very simple, just pass http.Client and resp.StatusCode. Such code has a clear structure and is easy to expand and maintain.

The above is the detailed content of Use the http.NewRequest function to create a new GET request object and set the request headers and parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn