Calling APIs with HttpClient

Calling APIs with HttpClient: HttpClient Basics, Reading JSON into Models, A Simple API Client.

Updated

  1. HttpClient Basics (Journeyman): HttpClient sends HTTP requests from C#. Give it a base address, await GetAsync for a response, check its status code, and read its body. A failed status is an answer to handle; a server that can't be reached throws an HttpRequestException.
  2. Reading JSON into Models (Journeyman): GetFromJsonAsync and ReadFromJsonAsync turn an API's JSON into your own model objects, matching names whatever their capitals. PostAsJsonAsync and PutAsJsonAsync send objects as JSON. Check the answer's status, and read the saved item from a 201 or a PUT's 200.
  3. A Simple API Client (Journeyman): Wrap an API in a client class, the way a DAO wraps a table. It's given an HttpClient, has one async method per endpoint, and turns 404 into null or false, so the rest of the program never sees a URL, a status code or JSON.