Go: how to make HTTP requests

Go: how to make HTTP requests

In this article, we'll explore how to make a simple HTTP request using Go's standard "net/http" package. We'll make a GET request to a web server and read the response.

Go (or Golang) is an open-source programming language created by Google that is rapidly gaining popularity due to its simplicity, efficiency, and ability to handle concurrency efficiently. One of the essential features that many developers use in Go is the ability to make HTTP requests to external servers to get or send data.

In this article, well explore how to make a simple HTTP request using Gos standard "net/http" package. Well make a GET request to a web server and read the response.

Lets start by writing the code to make an HTTP GET request to an external server and get the response data. We will use Gos "net/http" package to handle HTTP communication.


package main

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

func main() {
	// URL a cui effettuare la richiesta GET
	url := "https://api.example.com/data"

	// Eseguire la richiesta GET
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("Errore durante la richiesta GET:", err)
		return
	}
	defer resp.Body.Close()

	// Leggere la risposta
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Errore durante la lettura della risposta:", err)
		return
	}

	// Convertire i dati in stringa e stamparli
	fmt.Println("Risposta:", string(body))
}

In the above code, we have imported the necessary packages ("fmt", "net/http", and "io/ioutil"). We defined the URL to which we want to make the GET request. Then, we used the http.Get() function to make the GET request to that specific URL.

If the request is successful, we get the response in http.Response format. Using ioutil.ReadAll() , we read the response body into a byte array ( []byte ), which we then convert into a string for printing to the screen.

In the above code, we handled errors returned from the http.Get() function and from reading the response body. It is important to handle errors properly to ensure that our program is reliable and robust.

Often, you need to include parameters in the HTTP request, such as POST requests or GET requests with query parameters. Lets see an example where we make a GET request with query parameters:


package main

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

func main() {
	baseURL := "https://api.example.com/data"
	// Parametri di query
	params := "?param1=value1&param2=value2"

	// Componi l'URL completo con i parametri di query
	url := baseURL + params

	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("Errore durante la richiesta GET:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Errore durante la lettura della risposta:", err)
		return
	}

	fmt.Println("Risposta:", string(body))
}

Conclusions

In this article, weve learned how to make a simple HTTP GET request using Gos standard "net/http" package. Weve seen how to handle errors and how to include query parameters in our requests. Of course, Go offers many other features to further customize HTTP requests and handle responses in a more advanced way.