Skip to content

Commit af47c01

Browse files
authored
Merge pull request #73 from pyscript/fetch-update
Update to include response object handling.
2 parents 3c28bb7 + fd7d5ff commit af47c01

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

docs/user-guide/builtins.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,38 @@ 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+
The object returned from an `await fetch` call will have attributes that
144+
correspond to the
145+
[JavaScript response object](https://developer.mozilla.org/en-US/docs/Web/API/Response).
146+
This is useful for getting response codes, headers and other metadata before
147+
processing the response's data.
143148

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:
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+
```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
149164
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.
153168

154169
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)
155170
that you should simply pass in as keyword arguments like this:
@@ -161,8 +176,6 @@ from pyscript import fetch
161176
response = await fetch("https://example.com", method="POST", body="HELLO").text()
162177
```
163178

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-
166179
!!! Danger
167180

168181
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)