In Golang, we can use the Get
method in the net/http
package to send a GET request to the server to obtain resources. GET requests are usually used to obtain an existing resource, such as downloading files, obtaining data, etc.
The following is a simple example showing how to use Golang to send a GET request:
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { // 发送GET请求获取百度首页 resp, err := http.Get("https://www.baidu.com") if err != nil { fmt.Println("请求错误", err) return } // 记得在函数结束时关闭响应体 defer resp.Body.Close() // 读取响应体内容 body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("读取响应体错误", err) return } // 将响应体内容打印出来 fmt.Println(string(body)) }
In the code, we use http.Get
to send a GET request, and its The first parameter is the address of the resource to be obtained, and the returned result is a Response
object, which contains information such as response headers, response status, and response body. If an error occurs, such as the requested address is incorrect, the network connection times out, etc., http.Get
will return a non-empty error value err
.
Next, we use the ioutil.ReadAll
method to read the contents of the response body and print it out. It should be noted that the Body
field in the response object needs to be closed at the end of the program to release the resources it occupies.
In addition to using the http.Get
method, we can also use the http.NewRequest
method to manually construct a Request
object and set the request header. , request parameters and other information, and then use the http.DefaultClient.Do
method to send the request. The code example is as follows:
package main import ( "fmt" "net/http" "net/url" "strings" ) func main() { // 手动构造GET请求 values := url.Values{} values.Add("key", "value") apiUrl := "http://api.example.com?" + values.Encode() req, err := http.NewRequest("GET", apiUrl, nil) if err != nil { fmt.Println("请求错误", err) return } // 设置请求头信息 req.Header.Add("Accept-Encoding", "gzip, deflate, br") req.Header.Add("Connection", "keep-alive") req.Header.Add("Cookie", "user=abc; password=123") req.Header.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3") // 发送请求 client := http.DefaultClient resp, err := client.Do(req) if err != nil { fmt.Println("请求错误", err) return } defer resp.Body.Close() // 读取响应体 bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("读取响应体错误", err) return } bodyString := string(bodyBytes) fmt.Println(bodyString) }
In this example, we manually construct a GET request, use url.Values
to set the query parameters, and use the http.NewRequet
method to create A Request
object, and sets the request header information through the request header field. Next, we send the request object to http.DefaultClient
for request, and also use the ioutil.ReadAll
method to read the response body content.
In addition to using the http.Get
and http.NewRequest
methods, we can also use the third-party library gorequest
to quickly build and send HTTP request. This library provides a rich API that can easily set request headers, request parameters, JSON, file upload, etc.
The following is a simple usage example of gorequest
:
package main import ( "fmt" "github.com/parnurzeal/gorequest" ) func main() { // 使用gorequest构造GET请求 resp, body, err := gorequest.New().Get("https://www.baidu.com").End() if err != nil { fmt.Println("请求错误", err) return } fmt.Println(body) }
In this example, we use the gorequest.New
method to create a new Request the object, and then use the Get
method to set the request method and address. Finally, the End
method will send the request and return the response result. gorequest
provides many useful methods to flexibly set up and send requests according to your own needs.
To sum up, sending a GET request in Golang is very simple. We can use the http.Get
method, http.NewRequest
method or the third-party library gorequest
to send a request and get the response status and response body in the response object content. At the same time, we can also manually set request headers, request parameters and other information to meet the needs of different scenarios.
The above is the detailed content of get request in golang. For more information, please follow other related articles on the PHP Chinese website!