Home  >  Article  >  Backend Development  >  How to set HTTP request headers in Golang

How to set HTTP request headers in Golang

PHPz
PHPzOriginal
2023-04-05 10:29:452603browse

Golang, as a statically typed language, is favored by many developers for its performance in network communication. In actual project development, it is a very common requirement to initiate HTTP requests to external services. The request header is a very important part of the HTTP request. This article will discuss how to set the HTTP request header in Golang.

What are HTTP request headers?

HTTP request header is a part that carries some additional information when the client sends a request to the server. A request header usually consists of multiple parts such as request method, request URI, protocol version, request header field, etc. The request header field can carry some additional information for the server to use.

The request header field usually consists of a Key-Value form. For example, the Content-Type field in the request header has the Key as Content-Type and the Value as the type of the requested content. HTTP request headers are not fixed. We can define and modify request headers in actual development to meet different needs.

Set HTTP request headers in Golang

The standard library in Golang provides the net/http package, which provides some functions and structures for sending HTTP requests. We can set the HTTP request header by setting the Header field in the structure.

Create a request object through the http.NewRequest method

Use the http.NewRequest method to create a Request object, which has a Header field through which we can set the request header.

import (
  "net/http"
)

func main() {
  req, err := http.NewRequest("GET", "https://www.example.com", nil)
  if err != nil {
    ...
  }

  // 设置请求头
  req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
}

In the above code, we create a GET request object and assign it to req, and then set two request header fields through the req.Header.Set method : Content-Type and User-Agent.

Send a request through the http.Client method

We can also use the http.Client method to send an HTTP request with a request header.

import (
  "net/http"
)

func main() {
  client := &http.Client{}

  // 创造请求对象
  req, err := http.NewRequest("GET", "https://www.example.com", nil)
  if err != nil {
    ...
  }
  // 设置请求头
  req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")

  // 发送请求
  resp, err := client.Do(req)
  if err != nil {
    ...
  }
}

In the above code, we first create a http.Client object, then create a request object req, set the request header to the object, and finally use The client.Do method sends the request.

Create a request object through the http.NewRequestWithContext method

Starting from Go 1.13 version, Go provides a Context-based request method http.NewRequestWithContext(). This method can also be used to create a request object and also allows us to set request headers.

import (
  "net/http"
  "context"
)

func main() {
  ctx := context.Background()

  // 创造请求对象
  req, err := http.NewRequestWithContext(ctx, "GET", "https://www.example.com", nil)
  if err != nil {
    ...
  }
  
  // 设置请求头
  req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
}

In the above code, we used the context.Background() method to create a ctx object, and used the http.NewRequestWithContext method to create the req request object, and set the Request header.

Summary

To set HTTP request headers in Golang, we can use http.NewRequest, http.Client, http.NewRequestWithContext and other methods to create and send HTTP requests with request headers. Request header fields can be set in Key-Value form. In actual development, we can set request headers according to specific needs to meet different business requirements.

The above is the detailed content of How to set HTTP request headers 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