Lesson 3.2

Calling HTTP APIs

Parsing the body before checking the status is the most common mistake: an error page is not JSON, and the traceback you get points at the parser rather than the failed request.

7mBeginner19.7k students

Overview

The network is not a function call

Parsing the body before checking the status is the most common mistake: an error page is not JSON, and the traceback you get points at the parser rather than the failed request.

Set a timeout on every request. Without one, a hung server hangs your script indefinitely, and there is no default that saves you.

Transient failures deserve a retry with exponential backoff; a client error does not. Retrying a bad request just sends the same wrong thing repeatedly.

  • Check the status code first
  • Set a timeout on every call
  • Retry only on transient failures, with backoff

In this lesson you will:

  • Send a request and read the response
  • Check the status before parsing the body
  • Retry transient failures with backoff

Resources

Previous Lesson
Next Lesson
Calling HTTP APIs — Python Foundations for Data Work — Vertex