fetch
The fetch function of Azion Runtime: its parameters, the request options it accepts, redirect modes, timeouts, errors, and the Response it returns.
The fetch() function sends an HTTP request from your Azion Runtime code and returns a promise that resolves to a Response. You read that response, change it, or pass it back to the client through event.respondWith() or the return value of your handler. fetch() belongs to the Fetch API, which also defines the Request and Response objects, CORS, and the semantics of the HTTP Origin header. With these objects, your code handles, modifies, or builds requests and responses while it answers a request.
Use fetch() to forward a request to an origin server and return its response, or to call a REST or GraphQL API that supplies part of a response. You can also combine data from several backends in one function, or retrieve remote assets or configuration and rewrite them before delivery.
Syntax
| Parameter | Type | Description |
|---|---|---|
request | Request or string | Request to send: a Request object, or a string that holds the URL to fetch. |
init | Object (RequestInit) | Options of the request. |
The init object takes these options. method, headers, body, and redirect are the fields that the Request object documents, and signal cancels the request:
| Option | Type | Description |
|---|---|---|
method | String | Request method, such as POST. |
headers | Headers object | Headers of the request. |
body | — | Payload of the request, such as a JSON string. |
redirect | String | Redirect mode: follow, error, or manual. With follow, the default, fetch() follows the redirect and resolves to the final response. With manual, it resolves to the redirect response, such as a 301 with its location header. With error, the promise rejects. |
signal | AbortSignal | Signal that cancels the request, such as AbortSignal.timeout(300) to stop waiting after 300 ms. |
Limits
Each fetch() call runs inside the wall-clock ceiling of its invocation: 5 minutes, I/O wait included. One invocation can also make a limited number of outbound fetch() calls. For both values, refer to Functions limits.
To bound a single call, pass AbortSignal.timeout() as signal in init. The promise then rejects with an error whose name is TimeoutError, which your code catches.
Errors
The promise that fetch() returns rejects with these errors in a deployed function:
| Error | Cause | What to do |
|---|---|---|
TimeoutError: Signal timed out. | The AbortSignal.timeout() passed as signal expired before the response arrived. The error is a DOMException whose name is TimeoutError. | Catch the rejection and check error.name, or pass a longer timeout. |
TypeError: Fetch failed: Encountered redirect while redirect mode is set to 'error' | The server answered with a redirect, and redirect is error. | Set redirect to follow or manual, or catch the rejection. |
Example
This listener answers every request with the response that fetch() receives from https://example.com:
The listener returns a response with this status, this content type, and this body. The other response headers are cut, and the body is cut after its title: