Skip to content

Commit 8378989

Browse files
committed
Update docs on fetch.
1 parent 1b44a05 commit 8378989

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

docs/user-guide/builtins.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,34 +126,30 @@ in browsers with some important Pythonic differences.
126126

127127
The simple use case is to pass in a URL and `await` the response. Remember, in
128128
order to use `await` you must have the `async` attribute in the `script` tag
129-
that references your code.
129+
that references your code. If this request is in a function, that function
130+
should also be defined as `async`.
130131

131132
```python title="A simple HTTP GET with pyscript.fetch"
132133
from pyscript import fetch
133134

134135

135-
response = await fetch("https://example.com")
136+
response = await fetch("https://example.com").text()
136137
```
137138

138-
If the `fetch` operation _returns a response that is not deemed `OK`_ (the
139+
If the `fetch` operation _causes a response that is not deemed `OK`_ (the
139140
definition of which
140141
[can be found here](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok))
141142
then an exception is raised.
142143

143144
Assuming an `OK` response, the following methods are available to you to access
144145
the data returned from the server:
145146

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
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
148149
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.
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.
157153

158154
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)
159155
that you should simply pass in as keyword arguments like this:
@@ -162,7 +158,7 @@ that you should simply pass in as keyword arguments like this:
162158
from pyscript import fetch
163159

164160

165-
response = await fetch("https://example.com", method="POST", body="HELLO")
161+
response = await fetch("https://example.com", method="POST", body="HELLO").text()
166162
```
167163

168164
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()`.

0 commit comments

Comments
 (0)