Making HTTP Requests
GET, POST with http.NewRequest and http.DefaultClient
Making HTTP Requests is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
http.Get shortcut
http.Get(url) is the simplest way to make a GET request. Always close the response body.
resp, err := http.Get("https://api.example.com/users")
if err != nil { return err }
defer resp.Body.Close()http.DefaultClient
http.Get uses the package-level DefaultClient. For production, create a custom client with timeouts set.
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(url)http.NewRequest for full control
Use http.NewRequest to set method, headers, and body:
req, err := http.NewRequestWithContext(ctx, "POST", url, body)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)Request body
Pass a request body via an io.Reader. For JSON payloads, use bytes.NewBuffer(data) or strings.NewReader.
data, _ := json.Marshal(payload)
req, _ := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(data))Response status check
Always check the HTTP status code — a 4xx/5xx is not returned as a Go error by default.
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %d", resp.StatusCode)
}Decoding JSON response
Decode the response body with json.Decoder to avoid reading it all into memory first:
var result APIResponse
json.NewDecoder(resp.Body).Decode(&result)Query parameters
Build query strings with url.Values and attach them to the URL:
params := url.Values{}
params.Set("page", "1")
params.Set("limit", "20")
reqURL := baseURL + "?" + params.Encode()Client.Do and custom transport
Wrap http.DefaultTransport to add retry logic, custom TLS, or proxy settings without losing the default behaviour.
Response body must be closed
Failing to close the response body leaks the underlying TCP connection. Always use defer resp.Body.Close() even for error responses.
Following redirects
The default client follows up to 10 redirects. Set client.CheckRedirect to customise or disable redirect following.
Setting User-Agent
Set a descriptive User-Agent header so server logs can identify your client:
req.Header.Set("User-Agent", "myapp/1.0 (+https://myapp.com)")Quick Check
Why should you always close the HTTP response body?
Recap: Making HTTP Requests
Key points:
- Always defer resp.Body.Close()
- Create a custom http.Client with Timeout for production
- Use http.NewRequestWithContext for context and custom headers
- Check resp.StatusCode — HTTP errors are not Go errors
Frequently asked questions
Is the “Making HTTP Requests” lesson free?
Yes — the full text of “Making HTTP Requests” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Making HTTP Requests”?
GET, POST with http.NewRequest and http.DefaultClient You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Making HTTP Requests” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.