We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Pyfetch

In this course, we'll be using the pyfetch function from the pyodide.http module to make HTTP requests. We already used it in the last two assignments!

The pyfetch function is designed to work in WebAssembly environments and follows an API pattern similar to the browser's fetch API but adapted for Python.

Using Pyfetch

response = await pyfetch(url, method="GET", headers=headers)
response_data = await response.json()

We'll go in-depth on the various things happening in this standard pyfetch call later, but let's cover some basics for now.

  • response is the data that comes back from the server
  • url is the URL we are making a request to
  • method specifies the HTTP method to use (GET, POST, etc.)
  • headers is a dictionary containing HTTP headers to send with the request
  • await response.json() converts the JSON response data from the server into a Python object

Assignment

Fix the bug in the code.

The problem is that we aren't waiting for the response to come back before continuing with our code. We're calling .json on a coroutine instead of the actual response.

More on .json() and coroutines later.