FetchAPI
This page provides a simplified API reference for the app.fetch built-in HTTP client. Complete usage guidelines, examples, and best practices are available in the Built-in HTTP Client Guide.
app.fetch(input, init?)
Send an HTTP request. Signatures are compatible with native fetch, with additional support for timeouts, retries and requestId propagation.
Parameters
Return value: Promise<Response> — standard Fetch API Response object
app.fetch will trigger fetch:before, fetch:after, fetch:error hook; app.fetch.proxy will trigger proxy:before, proxy:after, proxy:error hook. For the complete payload and execution strategy, see Application instance hooks.
Shortcut method
app.fetch.get(url, init?)
Send a GET request.
app.fetch.post(url, body?, init?)
Send a POST request. When body is not null/undefined, JSON.stringify will be automatically set and Content-Type: application/json will be set; when body is empty, Content-Type will not be set and the request body will not be sent.
app.fetch.put(url, body?, init?)
Send a PUT request. body handling behaves the same as post (only sets Content-Type when body != null).
app.fetch.patch(url, body?, init?)
Send a PATCH request. body handling behaves the same as post (only sets Content-Type when body != null).
app.fetch.delete(url, init?)
Send a DELETE request.
app.fetch.create(options)
Create a preconfigured subclient instance. Subclients have independent baseURL, default headers, timeout and retry configuration.
Complete shortcut methods (get / post / put / patch / delete) and create() are also mounted on the sub-client, but proxy** will not be exposed. The proxy capability is only hung on the root app.fetch.proxy to avoid additional mental burden caused by app.fetch.create().proxy.
app.fetch.proxy
app.fetch.proxy is used to proxy the current request to the upstream service in the routing handler, and transparently transmit the upstream response directly to the client. It does not wrap 2xx / 3xx / 4xx / 5xx upstream responses as { code, data, requestId }; only proxy-local errors such as local parameter errors, target non-existence, upstream network errors, or timeouts return vext-style error responses.
Name the target agent
Named targets come from config.fetch.proxy[]:
name will be mapped to app.fetch.proxy.<name>, the reserved name then cannot be used.
Use app.fetch.proxy.<name>(req, res, options) when calling:
Direct URL proxy
When not using a named target, you can call app.fetch.proxy(req, res, { url }) directly:
header priority
Proxy request headers are merged in the following order, with the latter overriding the former:
Authorization will not passthrough from the current request by default. Passthrough of the original Authorization is only allowed if the target configuration or this call sets allowAuthorizationForward: true and forwardHeaders explicitly contains authorization.
retry contract
Agent retry configuration priority:
retry represents an additional number of attempts, so the total number of attempts is retry + 1. Only GET / HEAD / OPTIONS / PUT / DELETE these idempotent methods will automatically retry; POST / PATCH will not retry by default. Retryable conditions are network errors such as upstream 5xx or DNS/connection; 2xx/3xx/4xx does not retry, does not retry when timeout and returns local 504, and no longer writes a response when the client is disconnected.
Type definition
VextFetchInit
Inherited from standard RequestInit, extending the following fields:
VextFetchClientOptions
Configuration options for the create() factory method.
VextFetch
Type definition for root app.fetch. It is not only a callable function, but also has shortcut methods, create() and proxy mounted.
VextFetchProxyOptions
Use path for named target mode; use url for direct URL mode. If method is not passed, the current req.method will be used by default. When body is not passed, non-GET/HEAD requests will read the original body Buffer of the current req and forward it.
Global configuration
Configure global defaults through the fetch field in vext.config.ts:
- Global configuration
config.fetch.propagateHeaders: declare which headers need to be captured and transparently transmitted - Headers not declared in the global configuration: manually set in
init.headers - For details, see [Request context → Relationship with distributed tracing](/guide/request-context#Relationship with distributed tracing traceId) :::
Priority
Behavior description
Timeout
- Implemented using
AbortController+setTimeout Erroris thrown after timeout, message format:[app.fetch] GET https://... timed out after 10000ms- If
init.signalis passed in at the same time, it will be merged with the timeout signal - any trigger will abort the request
Try again
- Impotent methods only Retry:
GET/HEAD/OPTIONS/PUT/DELETE - POST / PATCH does not retry (to avoid repeated execution of side effects)
- Trigger condition: HTTP 5xx response or network error
- Not triggered: timeout (throw error directly), 4xx response
retryDelaysupports exponential backoff in functional form:(attempt) => Math.min(1000 * 2 ** attempt, 10000)
requestId propagation
- Automatically read the
requestIdof the current request fromrequestContext(AsyncLocalStorage) - Injected into the
x-request-idheader of outbound requests - Set
propagateRequestId: falseto disable
Structured log
Structured logging is automatically logged for every outbound request:
Log fields: type: "outbound" / method / url / status / duration / requestId
Replacement implementation
The current version does not expose the app.setFetch() public API, so direct replacement of the built-in implementation is not supported here.
If you need a different HTTP client strategy, it is recommended to keep app.fetch as the default implementation of the framework, and then additionally mount the custom client through a plug-in:
If app.fetch is completely bypassed, requestId propagation, timeouts, retries, and structured logging capabilities all need to be completed by yourself.
Type import
Next step
- Read the Built-in HTTP Client Guide for complete usage and best practices
- View the mounting location of
app.fetchin Application Instance - Understand the global configuration related to
fetchin Configuration Items