Home Backend Development Golang Use the net/http.Head function to send a HEAD request and get the response status code

Use the net/http.Head function to send a HEAD request and get the response status code

Jul 25, 2023 pm 12:29 PM
net/http Response status code head function

Use the net/http.Head function to send a HEAD request and get the response status code

In the Go language, we can use the functions provided by the net/http package to send HTTP requests and process HTTP responses. Among them, the Head function can send a HEAD request and return the response status code.

The following is a sample code that shows how to use the net/http.Head function to send a HEAD request and get the response status code:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    // 创建一个http.Client
    client := &http.Client{}

    // 创建一个HEAD请求
    req, err := http.NewRequest("HEAD", "https://www.example.com", nil)
    if err != nil {
        fmt.Println("创建请求失败:", err)
        return
    }

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

    // 获取响应状态码
    statusCode := resp.StatusCode
    fmt.Println("响应状态码:", statusCode)
}
Copy after login

In the above sample code, we first create a http.Client, and then uses the http.NewRequest function to create a HEAD request object. Next, we sent the request through the client.Do method and got the response object resp. Finally, we get the response status code through resp.StatusCode and print it out.

It should be noted that the request address in the above code is "https://www.example.com", you can replace it with the specific target URL.

Through the above code example, we can easily use the Head function of the net/http package to send a HEAD request and obtain the response status code. This is very useful in some scenarios where we only care about service availability, response time and other information.

The above is the detailed content of Use the net/http.Head function to send a HEAD request and get the response status code. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the net/http/httputil.DumpResponse function in golang to print HTTP response information How to use the net/http/httputil.DumpResponse function in golang to print HTTP response information Nov 18, 2023 am 09:35 AM

How to use the net/http/httputil.DumpResponse function in golang to print HTTP response information In golang, we can use the net/http package to send HTTP requests and receive HTTP responses. Sometimes, we need to view the details of the HTTP response, such as response headers, response body, etc. To achieve this purpose, the net/http/httputil package provides a useful DumpResponse function.

How to use the net/http/httputil.DumpRequest function in golang to print HTTP request information How to use the net/http/httputil.DumpRequest function in golang to print HTTP request information Nov 18, 2023 am 09:11 AM

Overview of how to use the net/http/httputil.DumpRequest function in golang to print HTTP request information: In Golang, you can use the httputil.DumpRequest function provided by the net/http package to print HTTP request information. This function can help us easily view the contents of the request header, request line, and request body. This article details how to use this function and provides specific code examples. Step 1: Guide

Master the net/http.PostForm function in Go language documentation to send form data Master the net/http.PostForm function in Go language documentation to send form data Nov 04, 2023 am 09:02 AM

Today, the Internet has become an extremely precious resource, and data needs to be sent at all times. To stay competitive in this fast-paced era, we need to master a variety of skills. Go language has become a very popular language currently. Sending data has become easier and more efficient in Go. This article introduces the net/http.PostForm function in the Go language documentation to send form data, providing readers with a simple method that allows programmers to run programs quickly and easily. HTTPPOS

Use the net/http.Head function to send a HEAD request and get the response status code Use the net/http.Head function to send a HEAD request and get the response status code Jul 25, 2023 pm 12:29 PM

Use the net/http.Head function to send a HEAD request and get the response status code. In the Go language, we can use the functions provided by the net/http package to send HTTP requests and process HTTP responses. Among them, the Head function can send a HEAD request and return the response status code. Here is a sample code that shows how to use the net/http.Head function to send a HEAD request and get the response status code: packagemainimport

Go language document interpretation: detailed explanation of net/http.NewServeMux function Go language document interpretation: detailed explanation of net/http.NewServeMux function Nov 03, 2023 pm 06:33 PM

Go is a fast and easy-to-use programming language with efficient memory management and good concurrency features. The net/http package in its standard library provides the implementation of HTTP client and server, and also provides many HTTP-related functions. Among them, the NewServeMux function is a very commonly used function. It can create an HTTP request router and bind the router to the HTTP server. This article will introduce NewServeMux in the net/http package

How to use the net/http/httptest.NewTLSServer function in golang to start a temporary HTTPS server How to use the net/http/httptest.NewTLSServer function in golang to start a temporary HTTPS server Nov 18, 2023 pm 02:48 PM

In the daily Go language development process, we often need to start an HTTPS server to test some security-related functions, especially when developing web applications. At this time, we can use the Go language's built-in net/http/httptest.NewTLSServer function to create a temporary HTTPS server for testing. So, this article will introduce how to use the NewTLSServer function to start a temporary HTTPS server and also

Learn the net/http.Post function in the Go language documentation to send a POST request Learn the net/http.Post function in the Go language documentation to send a POST request Nov 04, 2023 am 11:39 AM

Learning network programming in Go language is a very important part, and sending POST requests is an indispensable part. This article will introduce how to use the net/http.Post function in the Go language documentation to send a POST request, including specific code examples. First, we need to understand what a POST request is. It is a request method for sending data to the server. Unlike GET requests, POST requests can send more data and do not expose the data in the URL. Normally, we use P

How to simulate HTTP requests and responses using the net/http/httptest.NewRecorder function in golang How to simulate HTTP requests and responses using the net/http/httptest.NewRecorder function in golang Nov 18, 2023 pm 02:26 PM

How to simulate HTTP requests and responses using the net/http/httptest.NewRecorder function in golang When developing and testing web applications, it is often necessary to simulate HTTP requests and responses. Golang provides the net/http/httptest package to easily simulate HTTP requests and responses. Among them, the httptest.NewRecorder function is a very useful function, which can create a

See all articles