Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Task - Using an API | Asynchronous Programming and Introduction to Web Services
Introduction to .NET with C#
course content

Course Content

Introduction to .NET with C#

Introduction to .NET with C#

1. Introduction
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
4. CRUD Operations with MySQL
5. Optimizing Database Operations

book
Task - Using an API

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 2. Chapter 6
We're sorry to hear that something went wrong. What happened?
some-alt