How do you send an HTTP GET request with an HTTP client in Go?
The GET request method is idempotent, which means that making the same request multiple times will not change the state of the server or the resource being requested. It is also considered a safe method, as it does not modify any data on the server.
GET requests are often used to retrieve data from APIs, web services, and other types of servers that provide information to clients over the web. They can be sent using various programming languages and tools that support HTTP communication, such as cURL, Postman, or Go's built-in http package.
In Go, you can send an HTTP GET request using the net/http package. Here's an example:
Example
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// Create an HTTP client
client := &http.Client{}
// Create a new GET request
req, err := http.NewRequest("GET", "https://example.com/api/hello", nil)
if err != nil {
fmt.Println(err)
return
}
// Send the GET request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
// Print the response body
fmt.Println(string(body))
}
In this example, we create an HTTP client using &http.Client{} and create a new GET request using http.NewRequest(). We then send the GET request using client.Do(req) and read the response body using ioutil.ReadAll(resp.Body). Finally, we print the response body to the console using fmt.Println(). Note that we also need to close the response body using defer resp.Body.Close() to avoid resource leaks.
Example
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/hello", func(c *gin.Context) {
// Create an HTTP client
client := &http.Client{}
// Create a new GET request
req, err := http.NewRequest("GET", "https://example.com/api/hello", nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Send the GET request
resp, err := client.Do(req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Set the response content type and write the response body
c.Header("Content-Type", "application/json")
c.String(resp.StatusCode, string(body))
})
router.Run(":8080")
}
import (
"fmt"
"net/http"
"github.com/astaxie/beego"
)
func main() {
// Create an HTTP client
client := &http.Client{}
// Create a new GET request
req, err := http.NewRequest("GET", "https://example.com/api/hello", nil)
if err != nil {
fmt.Println(err)
return
}
// Send the GET request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
// Set the response content type and write the response body
beego.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json")
beego.Ctx.WriteString(string(body))
}