Home > Backend Development > Golang > How to Set Custom Headers in Go's HTTP GET Requests?

How to Set Custom Headers in Go's HTTP GET Requests?

Susan Sarandon
Release: 2024-12-17 09:58:24
Original
792 people have browsed it

How to Set Custom Headers in Go's HTTP GET Requests?

Setting Headers in HTTP GET Requests with Go

In a Go program, you can customize the headers of an HTTP GET request using the Header field of the Request struct. The syntax to set a header is:

req.Header.Set("header-name", "value")
Copy after login

Example code:

client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("X-My-Header", "value")
res, _ := client.Do(req)
Copy after login

This code sets the header with the key "X-My-Header" to the value "value". You can set multiple headers by calling Set multiple times or by using the Add method.

The above is the detailed content of How to Set Custom Headers in Go's HTTP GET Requests?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template