fetch
The fetch() method initiates an HTTP/HTTPS network request and returns a Promise that resolves to a Response object.
It behaves similarly to the standard Fetch API available in browsers but includes native extensions optimized for Scripting’s iOS runtime environment — such as local file access, Data integration, custom redirect handling, abort control, and debugging labels.
Definition
Parameters
1. url: string
The URL to request. It can be:
- A network resource, e.g.
"https://api.example.com/data" - A local file URL, e.g.
"file:///var/mobile/Containers/Data/Application/..."
2. init?: RequestInit
An optional configuration object used to customize the request’s method, headers, body, timeout, abort signal, and other options.
Parameter Details
Return Value
Returns a Promise<Response>.
The Response object represents the result of the request.
Even when the HTTP status code indicates failure (e.g., 404 or 500), fetch resolves successfully with a Response.
The Promise is rejected only when:
- The request cannot be completed (e.g., network error)
- The URL is malformed
- The request is aborted or times out
Error Handling
Examples
Example 1 — Basic GET Request
Example 2 — POST Request with JSON Body
Example 3 — Uploading Files via FormData
Example 4 — Timeout Handling
Example 5 — Aborting Requests with AbortController
Example 6 — Custom Redirect Handling
Example 7 — Debug Label for Logging
Relationship with Other Classes
Additional Details
-
Cookie Management:
fetchdoes not automatically store or send cookies. Cookies from responses can be accessed viaresponse.cookies. -
Redirects: Redirects are automatically followed unless overridden by
handleRedirect. -
Concurrency: Each request runs independently and safely in parallel.
-
File Support: You can upload local files using
Data.fromFile()as the request body.
Summary
The fetch() API in Scripting extends the standard web Fetch interface with native capabilities:
- Full iOS-native networking integration
- Support for
Databinary objects - File upload and download handling
- Timeout and abort control
- Custom redirect interception
- Local logging with
debugLabel
