Home  >  Article  >  Backend Development  >  Use the http.Client function to send a customized HTTP request, obtain the response status code and response content, and set the timeout.

Use the http.Client function to send a customized HTTP request, obtain the response status code and response content, and set the timeout.

王林
王林Original
2023-07-24 15:27:15824browse

Use the http.Client function to send customized HTTP requests and obtain the response status code and response content, and set the timeout period

In Go language, we can use net/http package to send HTTP requests and get responses. Among them, you can use the http.Client type to send customized HTTP requests and set the timeout.

Create HTTP client
First, we need to create an object of type http.Client for sending HTTP requests. HTTP transmission related parameters, such as proxy, TLS configuration, etc., can be set by setting the Transport field. By default, the http.DefaultTransport object is used to send requests, and the http.DefaultClient object is used.

The following is a sample code to create an HTTP client:

client := &http.Client{
    Timeout: time.Second * 10,
}

In the above code, we create an HTTP client object client and set the timeout to 10 Second.

Send HTTP request
Use the http.Client object to send an HTTP request. You can use the http.NewRequest function to create a new onehttp.Request object, and then use the Do method of http.Client to send the request and get the response.

The following is an example code for sending an HTTP request:

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

resp, err := client.Do(req)
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()

In the above code, we create a GET request with the target URL being https://example.com . Then, we use the client.Do(req) method to send the request and get the resp object as a response.

Get the response status code and content
We can use the resp.StatusCode field to get the status code of the HTTP response, use ioutil.ReadAllFunction to read the response content.

The following is a sample code to obtain the response status code and content:

statusCode := resp.StatusCode
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
    log.Fatal(err)
}

In the above code, we use resp.StatusCode to obtain the response status code, use ioutil.ReadAll function to read the response content and store it in the body variable.

Complete example
The following is a complete example that demonstrates how to use http.Client to send a customized HTTP request and obtain the response status code and Content, and set the timeout:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"
)

func main() {
    client := &http.Client{
        Timeout: time.Second * 10,
    }

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

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

    statusCode := resp.StatusCode
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Response status code:", statusCode)
    fmt.Println("Response body:", string(body))
}

In the above code, we created an HTTP client object client and set the timeout to 10 seconds. Then, we send a GET request and obtain the response status code and content, and print the output.

By using http.Client to send customized HTTP requests and setting the timeout, we can send HTTP requests more flexibly and process them accordingly based on the status code and content of the response. .

The above is the detailed content of Use the http.Client function to send a customized HTTP request, obtain the response status code and response content, and set the timeout.. 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