Skip to main content
Send Web Request

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()

PUT request example

Replacing a resource with PUT:

jsonBody := []byte("{\"name\":\"John Updated\",\"email\":\"john.new@example.com\"}")
req, _ := http.NewRequest("PUT", "https://api.example.com/users/123", bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)

DELETE request example

Deleting a resource:

req, _ := http.NewRequest("DELETE", "https://api.example.com/users/123", nil)
req.Header.Set("Authorization", "Bearer YOUR_TOKEN")
resp, err := client.Do(req)

Error handling

Check resp.StatusCode. Handle err from client.Do() for network errors. Always defer resp.Body.Close() to avoid leaks.

Try it in Send Web Request

Test your API in Send Web Request. Paste a curl command or build the request visually, then translate to Go.

Open Send Web Request

Other libraries you can try