pyodide.http#

Classes:

FetchResponse(url, js_response)

A wrapper for a Javascript fetch response.

Functions:

open_url(url)

Fetches a given URL synchronously.

pyfetch(url, **kwargs)

Fetch the url and return the response.

class pyodide.http.FetchResponse(url: str, js_response: pyodide.JsProxy)#

A wrapper for a Javascript fetch response.

See also the Javascript fetch Response api docs.

Parameters
  • url – URL to fetch

  • js_response – A JsProxy of the fetch response

property body_used: bool#

Has the response been used yet?

(If so, attempting to retrieve the body again will raise an OSError.)

async buffer() pyodide.JsProxy#

Return the response body as a Javascript ArrayBuffer

async bytes() bytes#

Return the response body as a bytes object

clone() pyodide.http.FetchResponse#

Return an identical copy of the FetchResponse.

This method exists to allow multiple uses of response objects. See Response.clone

async json(**kwargs: Any) Any#

Return the response body as a Javascript JSON object.

Any keyword arguments are passed to json.loads.

async memoryview() memoryview#

Return the response body as a memoryview object

property ok: bool#

Was the request successful?

property redirected: bool#

Was the request redirected?

property status: str#

Response status code

property status_text: str#

Response status text

async string() str#

Return the response body as a string

property type: str#

The type of the response.

async unpack_archive(*, extract_dir: Optional[str] = None, format: Optional[str] = None) None#

Treat the data as an archive and unpack it into target directory.

Assumes that the file is an archive in a format that shutil has an unpacker for. The arguments extract_dir and format are passed directly on to shutil.unpack_archive.

Parameters
  • extract_dir (str) – Directory to extract the archive into. If not provided, the current working directory is used.

  • format (str) – The archive format: one of “zip”, “tar”, “gztar”, “bztar”. Or any other format registered with shutil.register_unpack_format(). If not provided, unpack_archive() will use the archive file name extension and see if an unpacker was registered for that extension. In case none is found, a ValueError is raised.

property url: str#

The url of the response.

It may be different than the url passed to fetch.

pyodide.http.open_url(url: str) _io.StringIO#

Fetches a given URL synchronously.

The download of binary files is not supported. To download binary files use pyodide.http.pyfetch() which is asynchronous.

Parameters

url (str) – URL to fetch

Returns

the contents of the URL.

Return type

io.StringIO

async pyodide.http.pyfetch(url: str, **kwargs: Any) pyodide.http.FetchResponse#

Fetch the url and return the response.

This functions provides a similar API to the JavaScript fetch function however it is designed to be convenient to use from Python. The pyodide.http.FetchResponse has methods with the output types already converted to Python objects.

Parameters