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, js_response)#

A wrapper for a Javascript fetch Response.

Parameters:
body_used: bool#

Has the response been used yet?

If so, attempting to retrieve the body again will raise an OSError. Use clone() first to avoid this. See Response.bodyUsed.

async buffer()#

Return the response body as a Javascript ArrayBuffer.

See Response.arrayBuffer().

Return type:

JsBuffer

async bytes()#

Return the response body as a bytes object

Return type:

bytes

clone()#

Return an identical copy of the FetchResponse.

This method exists to allow multiple uses of FetchResponse objects. See Response.clone().

Return type:

FetchResponse

headers: dict[str, str]#

Response headers as dictionary.

async json(**kwargs)#

Treat the response body as a JSON string and use json.loads() to parse it into a Python object.

Any keyword arguments are passed to json.loads().

Parameters:

kwargs (Any) –

Return type:

Any

async memoryview()#

Return the response body as a memoryview object

Return type:

memoryview

ok: bool#

Was the request successful?

See Response.ok.

raise_for_status()#

Raise an OSError if the status of the response is an error (4xx or 5xx)

Return type:

None

redirected: bool#

Was the request redirected?

See Response.redirected.

status: int#

Response status code

See Response.status.

status_text: str#

Response status text

See Response.statusText.

async string()#

Return the response body as a string

Does the same thing as FetchResponse.text().

Return type:

str

Deprecated since version 0.24.0: Use FetchResponse.text() instead.

async text()#

Return the response body as a string

Return type:

str

type: str#

The type of the response.

See Response.type.

async unpack_archive(*, extract_dir=None, format=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 (Optional[str]) – Directory to extract the archive into. If not provided, the current working directory is used.

  • format (Optional[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.

Return type:

None

url: str#

The url of the response.

The value may be different than the url passed to fetch. See Response.url.

pyodide.http.open_url(url)#

Fetches a given URL synchronously.

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

It will not work in Node unless you include an polyfill for XMLHttpRequest.

Parameters:

url (str) – URL to fetch

Return type:

StringIO

Returns:

The contents of the URL.

Examples

>>> None 
>>> import pytest; pytest.skip("TODO: Figure out how to skip this only in node")
>>> url = "https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide-lock.json"
>>> url_contents = open_url(url)
>>> import json
>>> result = json.load(url_contents)
>>> sorted(list(result["info"].items()))
[('arch', 'wasm32'), ('platform', 'emscripten_3_1_45'), ('python', '3.11.3'), ('version', '0.24.1')]
async pyodide.http.pyfetch(url, **kwargs)#

Fetch the url and return the response.

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

Parameters:
Return type:

FetchResponse

Examples

>>> import pytest; pytest.skip("Can't use top level await in doctests")
>>> res = await pyfetch("https://cdn.jsdelivr.net/pyodide/v0.23.4/full/repodata.json")
>>> res.ok
True
>>> res.status
200
>>> data = await res.json()
>>> data
{'info': {'arch': 'wasm32', 'platform': 'emscripten_3_1_32',
'version': '0.23.4', 'python': '3.11.2'}, ... # long output truncated