

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Communicating on the Web
incomplete
2: HTTP Requests and Responses
incomplete
3: HTTP Powers Websites
incomplete
4: HTTP URLs
incomplete
5: Using URLs in HTTP
incomplete
6: Requests and Responses Quiz
incomplete
7: net/http
incomplete
8: Web Clients
incomplete
9: Web Servers
incomplete
This lesson's interactive features are locked, please to keep using them
In this course, we'll be using Go's standard net/http package and the http.Client to make HTTP requests. In fact, we've already been using it! The http.Get function uses the http.DefaultClient under the hood.
import (
"fmt"
"io"
"net/http"
)
func getProjects() ([]byte, error) {
res, err := http.Get("https://api.jello.com/projects")
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("error reading response: %w", err)
}
return data, nil
}
We'll go in-depth on the various things happening here later, but let's cover some basics for now.
http.Get uses the http.DefaultClient to make a request to the given urlres is the HTTP response that comes back from the serverdefer res.Body.Close() ensures that the response body is properly closed after reading. Not doing so can cause memory issues.io.ReadAll reads the response body into a slice of bytes []byte called dataThere is a bug in the getIssueData function! It's returning the entire http.Response instead of the data from the body (a slice of bytes). Fix it so that it returns []byte.