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
This is useful for getting response codes, headers and other metadata before
147
+
processing the response's data.
143
148
144
-
Assuming an `OK` response, the following methods are available to you to access
145
-
the data returned from the server:
149
+
Alternatively, rather than using a double `await` (one to get the response, the
150
+
other to grab the data), it's possible to chain the calls into a single
151
+
`await` like this:
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
+
```python title="A simple HTTP GET as a single await"
154
+
from pyscript import fetch
155
+
156
+
data =await fetch("https://example.com").text()
157
+
```
158
+
159
+
The following awaitable methods are available to you to access the data
160
+
returned from the server:
161
+
162
+
*`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.
163
+
*`blob()` returns a JavaScript [`blob`](https://developer.mozilla.org/en-US/docs/Web/API/Response/blob) version of the response. This is equivalent
149
164
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.
165
+
*`bytearray()` returns a Python [`bytearray`](https://docs.python.org/3/library/stdtypes.html#bytearray) version of the response.
166
+
*`json()` returns a Python datastructure representing a JSON serialised payload in the response.
167
+
*`text()` returns a Python string version of the response.
153
168
154
169
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
170
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
179
!!! Danger
167
180
168
181
You may encounter [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) errors (especially with reference to a missing
0 commit comments