Making GET Requests and Handling Responses

Welcome to the second lesson in our journey of interacting with APIs using Go. In the previous lesson, we established a strong understanding of RESTful APIs and how HTTP requests facilitate interactions with them. We used curl to interact directly with an API endpoint. Now, we will automate this process using Go's net/http package. This lesson will guide you through making HTTP requests in Go, equipping you with essential skills for web development and API integration.

Setting Up the Environment

To make HTTP requests in Go, we'll use the standard library's net/http package, which is powerful and does not require any third-party installation. If you're developing locally, make sure you have Go installed on your system. You can download and install it from the official website. Once installed, verify your installation by running:

This command should show the currently installed version of Go. With Go set up, you're ready to write and run scripts that automate HTTP requests, saving time and boosting efficiency.

Defining the Base URL

In Go, we'll define a base URL for the API service we are using. This approach keeps our code modular and maintainable.

By defining the base URL this way, we can easily append endpoints for different API services, keeping our code clean and adaptable.

Performing a Basic GET Request

Let's move on to fetching data from an API using Go's http.Get() function. Our goal is to retrieve a list of to-do items from the /todos endpoint.

Here, http.Get() sends the GET request, and io.ReadAll() reads the response body. We print the raw response as a string, giving us the immediate result returned by the server.

Handling Successful Requests (Status Code: 200)

When the server returns a 200 status code, it indicates a successful request. Below is an example of retrieving and displaying the response body.

In Go, http.StatusOK is interchangeable with its integer representation (200). Using the named constant improves readability and helps avoid magic numbers in your code, making it more maintainable and self-explanatory.

This ensures the response is successfully retrieved and displayed as a string. Here’s an example of what the raw response might look like:

By reading the response body and printing it, we can inspect the returned data before processing it further.

Handling Bad Requests (Status Code: 400)

If the request is malformed, a 400 status code is returned to signify this. Let's handle it gracefully in Go.

Handling Unauthorized Requests (Status Code: 401)

For cases involving a 401 Unauthorized status code, which typically means authorization issues, we'll handle them as follows.

Handling Not Found Errors (Status Code: 404)

A 404 status code indicates that the requested resource was not found.

Handling Internal Server Errors (Status Code: 500)

The 500 status code denotes an internal server error, often requiring server-side fixes.

Handling Unexpected Status Codes

Finally, for any unexpected status codes, we generalize error handling.

Conclusion, Key Takeaways, and Next Steps

In this lesson, we've explored how to use Go's net/http package to make GET requests, handle responses, and interpret HTTP status codes. This knowledge is crucial for building reliable API interactions. As you proceed to practice exercises, focus on implementing these code snippets and handling different status codes to deepen your understanding. In future lessons, we will build on this foundation to perform more complex tasks such as updating and manipulating API data. Keep progressing as you advance through the course!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal