Skip to content

Commit 9696f24

Browse files
committed
Update to include response object handling.
1 parent 3c28bb7 commit 9696f24

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

docs/user-guide/builtins.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,32 @@ should also be defined as `async`.
133133
from pyscript import fetch
134134

135135

136-
response = await fetch("https://example.com").text()
136+
response = await fetch("https://example.com")
137+
if response.ok:
138+
data = await response.text()
139+
else:
140+
print(response.status)
137141
```
138142

139-
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:
143146

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+
```
146152
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
149158
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.
153162
154163
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)
155164
that you should simply pass in as keyword arguments like this:
@@ -161,8 +170,6 @@ from pyscript import fetch
161170
response = await fetch("https://example.com", method="POST", body="HELLO").text()
162171
```
163172

164-
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-
166173
!!! Danger
167174

168175
You may encounter [CORS](https://developer.mozilla.org/en-US/docs/Glossary/CORS) errors (especially with reference to a missing

0 commit comments

Comments
 (0)