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.