You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user-guide/builtins.md
+68Lines changed: 68 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -116,6 +116,74 @@ Such named modules will always then be available under the
116
116
Please see the documentation (linked above) about restrictions and gotchas
117
117
when configuring how JavaScript modules are made available to PyScript.
118
118
119
+
### `pyscript.fetch`
120
+
121
+
A common task is to `fetch` data from the web via HTTP requests. The
122
+
`pyscript.fetch` function provides a uniform way to achieve this in both
123
+
Pyodide and MicroPython. It is closely modelled on the
124
+
[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) found
125
+
in browsers with some important Pythonic differences.
126
+
127
+
The simple use case is to pass in a URL and `await` the response. Remember, in
128
+
order to use `await` you must have the `async` attribute in the `script` tag
129
+
that references your code.
130
+
131
+
```python title="A simple HTTP GET with pyscript.fetch"
132
+
from pyscript import fetch
133
+
134
+
135
+
response =await fetch("https://example.com")
136
+
```
137
+
138
+
If the `fetch` operation _returns a response that is not deemed `OK`_ (the
139
+
definition of which
140
+
[can be found here](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok))
141
+
then an exception is raised.
142
+
143
+
Assuming an `OK` response, the following methods are available to you to access
144
+
the data returned from the server:
145
+
146
+
*`response.arrayBuffer()` returns a Python [memoryview](https://docs.python.org/3/library/stdtypes.html#memoryview) of the response. This is equivalent to the [`arrayBuffer()` method](https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer) in the browser based `fetch` API.
147
+
*`response.blob()` returns a JavaScript [`blob`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) version of the response. This is equivalent
148
+
to the [`blob()` method](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) in the browser based `fetch` API.
149
+
*`response.bytearray()` returns a Python [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray) version of the response.
150
+
*`response.json()` returns a Python datastructure representing a JSON serialised payload in the response.
151
+
*`response.text()` returns a Python string version of the response.
152
+
153
+
!!! warning
154
+
155
+
Unlike the browser based `fetch` API, you **do not need to await** the
156
+
methods listed above, in order to get the data from the response.
157
+
158
+
The underlying browser `fetch` API has [many request options](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options)
159
+
that you should simply pass in as keyword arguments like this:
Should you need access to the underlying [JavaScript response object](https://developer.mozilla.org/en-US/docs/Web/API/Response), you can find it as `response._response()`.
169
+
170
+
!!! Danger
171
+
172
+
You may encounter [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) errors (especially with reference to a missing
0 commit comments