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#

Task - Using an APITask - 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 url at which you make a request will depend upon the date which the user enters. It means that the URL will not be fixed, rather, it would follow a format. You can read about the format of this API at: the Numbers API website.

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.

Link to the Task: GitHub
1. You can make an HTTP request using an HttpClient object.
2. The response of the HTTP request can be stored in an HttpResponseMessage object.

public class NumbersFact
{
    public static async Task Main()
    {
        Console.WriteLine(">> This program gives you a random fact about any day of the year. ");

        Console.Write("Enter the number of month (1-12): ");
        int month = int.Parse(Console.ReadLine());

        Console.Write("Enter the number of day: ");
        int day = int.Parse(Console.ReadLine());

        // Write code below this line
        string url = $"http://numbersapi.com/{month}/{day}/date";
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        string responseContent = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Fact: " + responseContent);
        // Write code above this line
    }
}    
  

Все було зрозуміло?

Секція 2. Розділ 6
course content

Зміст курсу

Introduction to .NET with C#

Task - Using an APITask - 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 url at which you make a request will depend upon the date which the user enters. It means that the URL will not be fixed, rather, it would follow a format. You can read about the format of this API at: the Numbers API website.

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.

Link to the Task: GitHub
1. You can make an HTTP request using an HttpClient object.
2. The response of the HTTP request can be stored in an HttpResponseMessage object.

public class NumbersFact
{
    public static async Task Main()
    {
        Console.WriteLine(">> This program gives you a random fact about any day of the year. ");

        Console.Write("Enter the number of month (1-12): ");
        int month = int.Parse(Console.ReadLine());

        Console.Write("Enter the number of day: ");
        int day = int.Parse(Console.ReadLine());

        // Write code below this line
        string url = $"http://numbersapi.com/{month}/{day}/date";
        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        string responseContent = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Fact: " + responseContent);
        // Write code above this line
    }
}    
  

Все було зрозуміло?

Секція 2. Розділ 6
some-alt