How to create a custom HTTP request using the http.NewRequest function in golang

WBOY
Release: 2023-11-18 11:27:30
Original
1794 people have browsed it

How to create a custom HTTP request using the http.NewRequest function in golang

How to use the http.NewRequest function in golang to create a custom HTTP request

In golang, we can use the http.NewRequest function Create custom HTTP requests. This function allows us to more flexibly control all aspects of the request, including the request method, URL, request headers, request body, etc. Below we will detail how to use http.NewRequest to create a custom HTTP request and provide some code examples.

  1. Introduce the necessary packages

First, we need to introduce the net/http package:

import (
    "net/http"
)
Copy after login
  1. Create a custom HTTP request

We can create a custom HTTP request through the http.NewRequest function. The signature of the function is as follows:

func NewRequest(method, url string, body io.Reader) (*http.Request, error)
Copy after login

Among them, the method parameter represents the requested method, such as GET, POST, PUT, etc.; the url parameter represents the requested URL; ## The #body parameter represents the body of the request, which can be nil or an instance of the io.Reader interface.

Here is an example of how to create a simple GET request using the

http.NewRequest function:

url := "https://www.example.com"
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
    // 请求创建失败
    fmt.Println("创建请求失败:", err.Error())
    return
}
Copy after login

In this example, we create a GET request, And the requested URL is specified as "https://www.example.com". Through the

http.MethodGet constant, we can specify the request method as GET.

    Add a custom request header
Using the

req.Header.Add function, we can add a custom request header. Here is an example of how to add a custom User-Agent request header:

req.Header.Add("User-Agent", "My-Golang-Client")
Copy after login

In this example, we added a request header named "User-Agent" with a value of "My -Golang-Client".

    Send a custom HTTP request
Through the

Do method of http.Client, we can send a custom HTTP request and get the response. Here is an example of how to send a custom HTTP request and get the content of the response:

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

defer resp.Body.Close()

// 读取响应的内容
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    // 响应读取失败
    fmt.Println("读取响应失败:", err.Error())
    return
}

fmt.Println(string(body))
Copy after login
In this example, we create an instance of http.Client and pass it

DoThe method sends a custom HTTP request and gets the response.

After sending the request, we generally need to remember to close the body of the response to prevent resource leaks.

Summary

Through the

http.NewRequest function, we can create a custom HTTP request and send the request through the Do method of http.Client and get the response. Using a combination of these two functions, we can more flexibly control various aspects of the request.

The above is a detailed description of how to use the

http.NewRequest function in golang to create a custom HTTP request. Hope this article is helpful to you.

The above is the detailed content of How to create a custom HTTP request using the http.NewRequest function in golang. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template