The HTTP GET method is used for requesting data from the server or some other source. In this shot, we will learn how to fetch data with the GET method in Golang.
import ( “fmt” “net/http” )
The `net/http` package provides all the utilities that we need to make HTTP requests.
<h3>2. Make a GET request</h3>
The `net/http` package has a `Get()` method, which is used for making GET requests that accept a URL and return a response and an error. When the error is `nil`, the response returned will contain a response body and vice versa.
response, error := http.Get(“https://reqres.in/api/products”)
if error != nil { fmt.Println(error) }
fmt.Println(response)
> Note: At this point, the response contains a large amount of incoherent data, like the header and properties of the request.
<h3>Code</h3>
package mainimport ("fmt""net/http")func main() {// make GET requestresponse, error := http.Get("https://reqres.in/api/products")if error != nil {fmt.Println(error)}// print responsefmt.Println(response)}
As can be seen in the code given above, we made a GET request to this URL and then output the response on the console.