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
If the `fetch` operation _causes a response that is not deemed `OK`_ (the
140
-
definition of which
141
-
[can be found here](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok))
142
-
then an exception is raised.
143
+
Alternatively, rather than using a double `await` (one to get the response, the
144
+
other to grab the data), it's possible to chain the calls into a single
145
+
`await` like this:
143
146
144
-
Assuming an `OK` response, the following methods are available to you to access
145
-
the data returned from the server:
147
+
```python title="A simple HTTP GET as a single `await`"
148
+
from pyscript import fetch
149
+
150
+
data = await fetch("https://example.com").text()
151
+
```
146
152
147
-
*`await fetch("https://example.com").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.
148
-
*`await fetch("https://example.com").blob()` returns a JavaScript [`blob`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) version of the response. This is equivalent
153
+
The following awaitable methods are available to you to access the data
154
+
returned from the server:
155
+
156
+
* `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.
157
+
* `blob()` returns a JavaScript [`blob`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) version of the response. This is equivalent
149
158
to the [`blob()` method](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) in the browser based `fetch` API.
150
-
*`await fetch("https://example.com").bytearray()` returns a Python [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray) version of the response.
151
-
*`await fetch("https://example.com").json()` returns a Python datastructure representing a JSON serialised payload in the response.
152
-
*`await fetch("https://example.com").text()` returns a Python string version of the response.
159
+
* `bytearray()` returns a Python [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray) version of the response.
160
+
* `json()` returns a Python datastructure representing a JSON serialised payload in the response.
161
+
* `text()` returns a Python string version of the response.
153
162
154
163
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)
155
164
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()`.
165
-
166
173
!!! Danger
167
174
168
175
You may encounter [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) errors (especially with reference to a missing
0 commit comments