Home > Article > Backend Development > How to use the standard library to initiate HTTP requests in Go language
Go language (Golang) is an efficient programming language developed by Google, especially suitable for network programming and system programming. In web applications, sending HTTP requests is a very common operation. This article will introduce how to use the standard library to initiate HTTP requests in the Go language.
Before initiating an HTTP request, you must first understand the various parts of the HTTP request.
HTTP request consists of three parts: request line, request header and request body.
The request line contains the requested method, URL and protocol version, for example:
GET /api/users HTTP/1.1
The request header contains the metadata of the HTTP request, for example:
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 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8
The request body contains the application The data to be submitted, for example:
{"username":"admin","password":"123456"}
The following is an example of using the standard library to initiate an HTTP GET request Example:
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { resp, err := http.Get("https://www.example.com") if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Println(string(body)) }
In the above example, we initiated an HTTP GET request using the http.Get
function. This function returns a http.Response
type structure, which contains various parts of the HTTP response. We use the ioutil.ReadAll
function to read the contents of the response body and output it to the console.
In addition to the HTTP GET request, we can also use the standard library to initiate an HTTP POST request. Here is an example of making an HTTP POST request using the standard library:
package main import ( "bytes" "fmt" "io/ioutil" "net/http" ) func main() { url := "http://www.example.com/api/login" data := []byte(`{"username":"admin","password":"123456"}`) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) if err != nil { panic(err) } req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } fmt.Println(string(body)) }
In the above example, we created an HTTP request using the http.NewRequest
function. We specified the request method as POST, the URL as http://www.example.com/api/login
, and set the request body as {"username":"admin"," password":"123456"}
. We also set the Content-Type of the request header to application/json. Finally, we use the client.Do
method to perform an HTTP POST request and read the contents of the response body.
In addition to the standard library, we can also use third-party libraries to initiate HTTP requests. Here is an example of making an HTTP GET request using the third-party library github.com/go-resty/resty
:
package main import ( "fmt" "github.com/go-resty/resty/v2" ) func main() { client := resty.New() resp, err := client.R(). EnableTrace(). Get("https://www.example.com") if err != nil { panic(err) } fmt.Println(string(resp.Body())) }
In the above example, we used github. The
Get method provided by the com/go-resty/resty
library initiates an HTTP GET request. We also used the EnableTrace
method to print the details of the HTTP request.
Through the introduction of this article, we have learned how to use the standard library or third-party library to initiate HTTP requests in the Go language. Understanding the basic structure and usage of HTTP requests is very important for developing web applications. In actual development, we also need to consider issues such as the security and performance of HTTP requests.
The above is the detailed content of How to use the standard library to initiate HTTP requests in Go language. For more information, please follow other related articles on the PHP Chinese website!