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

1class Response {
2  readonly body: ReadableStream<Data>
3
4  get bodyUsed(): boolean
5  get cookies(): Cookie[]
6  json(): Promise<any>
7  text(): Promise<string>
8  data(): Promise<Data>
9  bytes(): Promise<Uint8Array>
10  arrayBuffer(): Promise<ArrayBuffer>
11  formData(): Promise<FormData>
12  get status(): number
13  get statusText(): string
14  get headers(): Headers
15  get ok(): boolean
16  get url(): string
17  get mimeType(): string | undefined
18  get expectedContentLength(): number | undefined
19  get textEncodingName(): string | undefined
20}

Properties

Property Type Description
body ReadableStream<Data> The response body as a readable stream of Data chunks.
bodyUsed boolean Indicates whether the response body has been read.
cookies Cookie[] A list of cookies sent by the server via the Set-Cookie header.
status number The HTTP status code of the response (e.g. 200, 404, 500).
statusText string The status message returned by the server (e.g. "OK", "Not Found").
headers Headers A Headers object representing response headers.
ok boolean true if the status code is between 200 and 299, otherwise false.
url string The final URL of the response after any redirects.
mimeType string undefined
expectedContentLength number undefined
textEncodingName string undefined

Methods

json(): Promise<any>

Parses the response body as JSON.

Example

1const response = await fetch("https://api.example.com/user")
2const data = await response.json()
3console.log(data.name)

text(): Promise<string>

Reads the response body as a UTF-8 string (or using textEncodingName if available).

Example

1const response = await fetch("https://example.com/message.txt")
2const text = await response.text()
3console.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

1const response = await fetch("https://example.com/image.png")
2const imageData = await response.data()
3FileManager.write(imageData, "/local/image.png")

bytes(): Promise<Uint8Array>

Reads the response as a byte array (Uint8Array).

Example

1const response = await fetch("https://example.com/file.bin")
2const bytes = await response.bytes()
3console.log("Received", bytes.length, "bytes")

arrayBuffer(): Promise<ArrayBuffer>

Reads the response as an ArrayBuffer, useful for low-level binary operations.

Example

1const response = await fetch("https://example.com/file")
2const buffer = await response.arrayBuffer()
3console.log(buffer.byteLength)

formData(): Promise<FormData>

Parses the response body as form data (for responses with multipart/form-data content).

Example

1const response = await fetch("https://example.com/form")
2const form = await response.formData()
3console.log(form.get("username"))

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.

1interface Cookie {
2  name: string
3  value: string
4  domain: string
5  path: string
6  isSecure: boolean
7  isHTTPOnly: boolean
8  isSessionOnly: boolean
9  expiresDate?: Date | null
10}
Field Type Description
name string Cookie name.
value string Cookie value.
domain string Domain to which the cookie belongs.
path string Path scope of the cookie.
isSecure boolean true if the cookie is sent only over HTTPS.
isHTTPOnly boolean true if inaccessible to JavaScript.
isSessionOnly boolean true if the cookie is temporary and expires at session end.
expiresDate Date | null Expiration date of the cookie, if specified.

Example — Reading Cookies

1const response = await fetch("https://example.com/login")
2for (const cookie of response.cookies) {
3  console.log(`${cookie.name} = ${cookie.value}`)
4}

By default, Scripting’s fetch() does not automatically store or send cookies. To reuse cookies across multiple requests, you can manually include them:

1const response = await fetch("https://example.com/login")
2const cookies = response.cookies
3const cookieHeader = cookies.map(c => `${c.name}=${c.value}`).join("; ")
4
5const next = await fetch("https://example.com/dashboard", {
6  headers: { "Cookie": cookieHeader },
7})

This gives you explicit cookie control similar to browser developer tools.


Relationship with Other Classes

Class Description
Request Represents the HTTP request that produced this response.
Headers Manages response headers.
Data Represents binary data returned from the response body.
FormData Used when the response contains multipart/form-data.
Cookie Represents a parsed HTTP cookie object.

Examples

Example 1 — Handling JSON API Responses

1const response = await fetch("https://api.example.com/profile")
2if (response.ok) {
3  const user = await response.json()
4  console.log(user.email)
5} else {
6  console.log("Error:", response.status, response.statusText)
7}

Example 2 — Downloading Binary Data

1const response = await fetch("https://example.com/photo.jpg")
2const fileData = await response.data()
3FileManager.write(fileData, "/local/photo.jpg")

Example 3 — Reading Cookies from a Response

1const response = await fetch("https://example.com/login")
2for (const cookie of response.cookies) {
3  console.log(`Cookie: ${cookie.name} = ${cookie.value}`)
4}

1const loginResponse = await fetch("https://example.com/login", {
2  method: "POST",
3  body: JSON.stringify({ username: "Tom", password: "1234" }),
4  headers: { "Content-Type": "application/json" },
5})
6
7// Build a cookie header manually
8const cookieHeader = loginResponse.cookies.map(c => `${c.name}=${c.value}`).join("; ")
9
10// Use cookies in a new request
11const dashboard = await fetch("https://example.com/dashboard", {
12  headers: { "Cookie": cookieHeader },
13})
14console.log(await dashboard.text())

Example 5 — Inspecting Metadata

1const response = await fetch("https://example.com/video.mp4")
2console.log("MIME Type:", response.mimeType)
3console.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