The Scripting app provides a simulated web-based fetch
interface that aligns with the Web Fetch API specification. This API enables performing network requests and handling responses in a modern, promise-based manner. It supports key features such as headers management, request cancellation, and form submission with multipart/form-data
.
The fetch()
method initiates an HTTP request to a network resource and returns a Promise
that resolves to a Response
object.
Unlike traditional XMLHttpRequest
, fetch()
uses promises and does not reject on HTTP protocol errors such as 404 or 500. Instead, these are reflected in the Response.ok
and Response.status
properties.
Request
ClassRepresents an HTTP request.
url: string
– The request URL.method: string
– HTTP method (GET, POST, PUT, DELETE, etc.).headers: Headers
– HTTP headers.body?: Data | FormData | string | ArrayBuffer
– The request body.connectTimeout?: number
– Timeout for establishing connection (in milliseconds).receiveTimeout?: number
– Timeout for receiving the response (in milliseconds).signal?: AbortSignal
– Signal to abort the request.cancelToken?: CancelToken
(deprecated) – Used for request cancellation.debugLabel?: string
– Custom label shown in the debug log.Response
ClassRepresents the response to a fetch()
request.
body: ReadableStream<Data>
– The response body as a stream.bodyUsed: boolean
– Indicates whether the body has been read.status: number
– HTTP status code.statusText: string
– HTTP status text.headers: Headers
– Response headers.ok: boolean
– true
if status is in the range 200–299.url: string
– The final URL after redirects.mimeType?: string
expectedContentLength?: number
textEncodingName?: string
json(): Promise<any>
– Parses body as JSON.text(): Promise<string>
– Parses body as text.data(): Promise<Data>
– Returns the response body as Data
.bytes(): Promise<Uint8Array>
– Returns the body as a Uint8Array
.arrayBuffer(): Promise<ArrayBuffer>
– Returns the body as an ArrayBuffer
.formData(): Promise<FormData>
– Parses body as FormData
.Headers
ClassManages HTTP headers.
append(name: string, value: string): void
get(name: string): string | null
has(name: string): boolean
set(name: string, value: string): void
delete(name: string): void
forEach(callback: (value: string, name: string) => void): void
keys(): string[]
values(): string[]
entries(): [string, string][]
toJson(): Record<string, string>
FormData
ClassRepresents multipart/form-data
payloads for form submissions.
append(name: string, value: string): void
append(name: string, value: Data, mimeType: string, filename?: string): void
get(name: string): string | Data | null
getAll(name: string): any[]
has(name: string): boolean
delete(name: string): void
set(name: string, value: string | Data, filename?: string): void
forEach(callback: (value: any, name: string, parent: FormData) => void): void
entries(): [string, any][]
Converts a FormData
object into a plain JSON object.
AbortController
and AbortSignal
Support aborting requests using signals.
Use signal
in a fetch request:
AbortError
Thrown when an operation is aborted using AbortSignal
.
CancelToken
Legacy API for canceling requests.
useCancelToken()
React-style hook to manage CancelToken
lifecycle in function components.
fetch()
will reject on network or CORS errors, not HTTP status errors.response.ok
or response.status
to inspect HTTP-level errors.AbortController
for modern cancellation handling.