Home  >  Article  >  Backend Development  >  How to request cookies in Golang

How to request cookies in Golang

PHPz
PHPzOriginal
2023-04-10 14:17:491157browse

Golang Request Cookie

As a programmer, we often involve network requests. When implementing network request functions, we often need to use cookies. So, how to request cookies in Golang? Next, this article will introduce how to request cookies in Golang.

First of all, we need to understand what Cookies are. Cookie is a data transmission method in the HTTP protocol, used to save the client's status information. It can record the user's login information, browsing history, shopping cart and other information, thereby realizing the tracking and management of user information.

In Golang, requesting cookies is divided into two parts: setting cookies and getting cookies.

Set Cookie:

In Golang request, you can set Cookie by setting header. In some websites that require login, it is necessary to carry cookies in the request header to illegally obtain data.

Sample code:

func setCookie() {
    url := "https://www.example.com/login"
    req, err := http.NewRequest("POST", url, strings.NewReader(`{"foo":"bar"}`))
    if err != nil {
        log.Fatalln(err)
    }
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    //设置Cookie
    req.AddCookie(&http.Cookie{Name: "user", Value: "123"})
    cli := http.Client{}
    resp, err := cli.Do(req)
    if err != nil {
        log.Fatalln(err)
    }
    defer resp.Body.Close()
    fmt.Println(resp.StatusCode)
}

In the example, we first construct the request body of a post request, set the Content-Type in the request header, and then set it through the req.AddCookie() method A cookie named user with a value of 123 is created.

Get Cookie:

In Golang request, Cookie can be obtained through the Request object. When performing some business logic operations, we need to obtain cookies from the link request header to obtain some website information.

Sample code:

func getCookie() {
    url := "https://www.example.com/home"
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        log.Fatalln(err)
    }
    cli := http.Client{}
    resp, err := cli.Do(req)
    if err != nil {
        log.Fatalln(err)
    }
    defer resp.Body.Close()
    cookies := resp.Cookies()
    for _, c := range cookies {
        fmt.Printf("cookieName=%s; cookieValue=%s\n", c.Name, c.Value)
    }
}

In the example, we first construct the request body of a GET request, then obtain the cookies in the response body by calling the resp.Cookies() method, and finally add The obtained cookies are output to the console.

Summary:

Through the above sample code, we can see that the operation of requesting cookies in Golang is very simple. By setting the Request object or constructing the request header, we can set and obtain Cookies. In actual development, we can learn from these techniques to implement more complex business logic operations.

The above is the detailed content of How to request cookies in Golang. 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