How to Make an HTTP Request with Go
Go's net/http package provides everything needed for HTTP requests. It's simple, fast, and part of the standard library.
GET request example
Making a GET request to fetch data with custom headers:
req, _ := http.NewRequest("GET", "https://api.example.com/users", nil)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_TOKEN")
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)POST request example
Making a POST request to send JSON data:
jsonBody := []byte("{\"name\":\"John\",\"email\":\"john@example.com\"}")
req, _ := http.NewRequest("POST", "https://api.example.com/users", bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
defer resp.Body.Close()Other libraries you can try
- Python:
Requests— simple and popular HTTP library,HTTPX— async-capable HTTP client - JavaScript:
Axios— promise-based HTTP client,node-fetch— Fetch API for Node.js - Ruby:
HTTParty— simple HTTP client,Faraday— flexible HTTP library,RestClient— lightweight REST client - Java:
OkHttp— efficient HTTP client,Apache HttpClient— feature-rich library - C#:
RestSharp— simple REST client,Flurl— fluent HTTP library - Go:
resty— simple HTTP client,grequests— goroutine-based requests - PHP:
Guzzle— popular PHP HTTP client,Symfony HttpClient— component-based HTTP client