Exercise: Talking to REST APIs over the Internet

Test your coding skills on REST API.

Challenge

In this exercise, your challenge is to create an application that talks to a REST API over the Internet.

Problem Statement

Use NASA’s public APOD (Astronomy Picture of the Day) to download today’s image.

Coding Challenge

Develop an application that obtains the URL for today’s Astronomy Picture of the Day using NASA’s REST API, then use this URL to download the image and save it to the temporary directory in your machine.

This is the URL for NASA’s APOD API. The API responds with a JSON body containing many fields. You’re interested in the two fields required to download the current image: media_type and url. Configure your response type to handle those fields appropriately.

Then, create a function to instantiate a http.Client that times out after 10 seconds. Use this client in two functions: getAPODInfo to obtain the URL information for the current image, and downloadImage to download and save the image. Some boilerplate code and function stubs are provided as a starting point.

If you feel stuck, refer to how you created an API client in this Chapter, or consult the documentation for net/http package. If you still need help, check the solution review in the next lesson. Good luck!

Get hands-on with 1200+ tech skills courses.