Response
The Response class represents the result of an HTTP request made using the fetch() API.
It provides access to the response body, headers, cookies, and metadata such as the status code and MIME type.
In the Scripting app, the Response API extends the standard Fetch API behavior to provide native-level enhancements, including:
- Access to structured cookie data
- Binary data handling via the
Data type
- Support for response streaming (
ReadableStream<Data>)
- Access to expected content length, MIME type, and text encoding
Definition
class Response {
readonly body: ReadableStream<Data>
get bodyUsed(): boolean
get cookies(): Cookie[]
json(): Promise<any>
text(): Promise<string>
data(): Promise<Data>
bytes(): Promise<Uint8Array>
arrayBuffer(): Promise<ArrayBuffer>
formData(): Promise<FormData>
get status(): number
get statusText(): string
get headers(): Headers
get ok(): boolean
get url(): string
get mimeType(): string | undefined
get expectedContentLength(): number | undefined
get textEncodingName(): string | undefined
}
Properties
Methods
json(): Promise<any>
Parses the response body as JSON.
Example
const response = await fetch("https://api.example.com/user")
const data = await response.json()
console.log(data.name)
text(): Promise<string>
Reads the response body as a UTF-8 string (or using textEncodingName if available).
Example
const response = await fetch("https://example.com/message.txt")
const text = await response.text()
console.log(text)
data(): Promise<Data>
Reads the response body as a binary Data object, which can be used for file saving, image decoding, or Base64 conversion.
Example
const response = await fetch("https://example.com/image.png")
const imageData = await response.data()
FileManager.write(imageData, "/local/image.png")
bytes(): Promise<Uint8Array>
Reads the response as a byte array (Uint8Array).
Example
const response = await fetch("https://example.com/file.bin")
const bytes = await response.bytes()
console.log("Received", bytes.length, "bytes")
arrayBuffer(): Promise<ArrayBuffer>
Reads the response as an ArrayBuffer, useful for low-level binary operations.
Example
const response = await fetch("https://example.com/file")
const buffer = await response.arrayBuffer()
console.log(buffer.byteLength)
Parses the response body as form data (for responses with multipart/form-data content).
Example
const response = await fetch("https://example.com/form")
const form = await response.formData()
console.log(form.get("username"))
cookies: Cookie[]
The cookies property provides direct access to the cookies set by the server via Set-Cookie headers.
Each cookie is represented by a Cookie object with structured metadata.
Cookie Type Definition
interface Cookie {
name: string
value: string
domain: string
path: string
isSecure: boolean
isHTTPOnly: boolean
isSessionOnly: boolean
expiresDate?: Date | null
}
Example — Reading Cookies
const response = await fetch("https://example.com/login")
for (const cookie of response.cookies) {
console.log(`${cookie.name} = ${cookie.value}`)
}
Example — Manual Cookie Management
By default, Scripting’s fetch() does not automatically store or send cookies.
To reuse cookies across multiple requests, you can manually include them:
const response = await fetch("https://example.com/login")
const cookies = response.cookies
const cookieHeader = cookies.map(c => `${c.name}=${c.value}`).join("; ")
const next = await fetch("https://example.com/dashboard", {
headers: { "Cookie": cookieHeader },
})
This gives you explicit cookie control similar to browser developer tools.
Relationship with Other Classes
Examples
Example 1 — Handling JSON API Responses
const response = await fetch("https://api.example.com/profile")
if (response.ok) {
const user = await response.json()
console.log(user.email)
} else {
console.log("Error:", response.status, response.statusText)
}
Example 2 — Downloading Binary Data
const response = await fetch("https://example.com/photo.jpg")
const fileData = await response.data()
FileManager.write(fileData, "/local/photo.jpg")
Example 3 — Reading Cookies from a Response
const response = await fetch("https://example.com/login")
for (const cookie of response.cookies) {
console.log(`Cookie: ${cookie.name} = ${cookie.value}`)
}
Example 4 — Manual Cookie Persistence Between Requests
const loginResponse = await fetch("https://example.com/login", {
method: "POST",
body: JSON.stringify({ username: "Tom", password: "1234" }),
headers: { "Content-Type": "application/json" },
})
// Build a cookie header manually
const cookieHeader = loginResponse.cookies.map(c => `${c.name}=${c.value}`).join("; ")
// Use cookies in a new request
const dashboard = await fetch("https://example.com/dashboard", {
headers: { "Cookie": cookieHeader },
})
console.log(await dashboard.text())
const response = await fetch("https://example.com/video.mp4")
console.log("MIME Type:", response.mimeType)
console.log("Expected Length:", response.expectedContentLength)
Summary
The Response class in Scripting offers a rich and extensible interface for handling HTTP responses:
- Compatible with the standard Fetch API
- Adds native cookie parsing and management
- Supports binary data streams via the
Data type
- Provides full access to headers, MIME type, and encoding
- Allows seamless use with
FormData and ReadableStream