Programming foundations

Stage 6 · Lesson 4 of 8

Calling a public API with HttpClient

Build and ship your first projects

sends HTTP requests. Create one instance and reuse it for the life of the program, because creating one per request can use up the computer's network (connection slots). await client.GetAsync(url) returns a response for any .

So check response.IsSuccessStatusCode, or call EnsureSuccessStatusCode(), before the body. Then parse it with await response.Content.ReadFromJsonAsync<Weather>(). 401 means a bad key, 404 a wrong URL and 429 too many requests.

Set client.Timeout so a slow server cannot hang your app, and catch HttpRequestException for network failures. Every call is , so await it all the way up. Wrap the calls in one class that returns your own records.

A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with

  • GitHub repoFree
    Project Based Learning

    Community maintained · Beginner

    Follow one guided build in your language, then build your own without a guide

Keep handy

  • Free APIs that give a small project real data to work with

Also worth your time

Mark this stage as not done?