Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Using an API | Section
Introduction to .NET with C#

bookChallenge: Using an API

Swipe to show menu

There is a free public API, it is called Numbers API. It has multiple endpoints, one of which returns a random fact about any date of the year.

The base code is given. The program takes a month number and a day number from the user.

Task: Make a request to the Numbers API to retrieve a random fact about that date.

Guidelines:

The format of retrieving a fact about some date is: http://numbersapi.com/<month>/<day>/date where <month> represents the number of the month and <day> represents the number of the day. For example, if the user enters 11 for the month (which represents the 11th month - November) and 6 for the day, the URL will be: http://numbersapi.com/11/6/date

Steps:

  • Create a new variable called url and store the URL following the format explained in the Guidelines. You might need to use string formatting;
  • Create a new HttpClient() object;
  • Use the HttpClient object and make a GET request to the url;
  • Store the response of the HTTP request into a HttpResponseMessage object;
  • Extract the raw message text from the HttpResponseMessage object using ReadAsStringAsync method;
  • Display the message.

Note

This task does not have unit tests. You can verify the correctness of your code based on a successful response from the API.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 15

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

SectionΒ 1. ChapterΒ 15
some-alt