Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions testing/script/response/response-object.mdx
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
---
title: "Response Object"
title: "Response object"
description: "Access response data including body, headers, status, and more in your scripts"
---

The `res` object that is available inside the [vars](../vars), [assertions](../../tests/assertions), [scripting](../getting-started) and [testing](../../tests/introduction)
contexts can be used to extract values from the response body, headers and status.
The `res` object is available inside the [vars](../vars), [assertions](../../tests/assertions), [scripting](../getting-started), and [testing](../../tests/introduction) contexts. Use it to extract values from the response body, headers, and status.

*Note that the `res` object is only available in the context of a request.*
*The `res` object is only available in the context of a request.*

You can also access it with [response queries](./response-query).
You can also query nested response data using [response queries](./response-query).

## Object Structure
## Properties

The `res` object has the following properties:
| Property | Description |
|---|---|
| `res.status` | HTTP status code (e.g., `200`, `404`) |
| `res.statusText` | HTTP status text (e.g., `"OK"`, `"Not Found"`) |
| `res.headers` | Key-value pairs of HTTP response headers |
| `res.body` | The parsed response body (string, object, etc.) |
| `res.responseTime` | Time in milliseconds the request took to complete |
| `res.url` | The full URL of the request |

- `body`: Represents the response body containing data returned to the client.
- `headers`: Contains key-value pairs representing HTTP headers associated with the response.
- `status`: Represents the HTTP status code indicating the outcome of the request.
## Methods

## Property Descriptions
| Method | Description |
|---|---|
| `res.getStatus()` | Returns the HTTP status code |
| `res.getStatusText()` | Returns the HTTP status text |
| `res.getHeader(name)` | Returns the value of a specific response header |
| `res.getHeaders()` | Returns all response headers |
| `res.getBody()` | Returns the response body |
| `res.getResponseTime()` | Returns the response time in milliseconds |
| `res.getUrl()` | Returns the request URL |
| `res.setBody(data)` | Replaces the response body with new data |
| `res.getSize()` | Returns an object with `header`, `body`, and `total` byte sizes |

### `body`

The `body` property of the `res` object contains the response data sent to the client. It can be a string, an object, or a stream, depending on the application's needs.

### `headers`

The `headers` property contains HTTP headers associated with the response. These headers provide metadata about the response, such as content type, encoding, and caching directives.

### `status`

The `status` property represents the HTTP status code of the response. It indicates the outcome of the request, such as success, redirection, client error, or server error.

## Example Usage
## Example usage

```javascript
// Example response object
const res = {
body: '{"message": "Hello, world!"}',
headers: {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
},
status: 200,
};

// Accessing response properties
console.log(res.body); // Output: '{"message": "Hello, world!"}'
console.log(res.headers['Content-Type']); // Output: 'application/json'
console.log(res.status); // Output: 200
let status = res.status; // 200
let statusText = res.statusText; // "OK"
let contentType = res.headers['content-type']; // "application/json"
let body = res.body; // { message: "Hello, world!" }
let time = res.responseTime; // 120
let url = res.url; // "https://api.example.com/data"

// Using getter methods
let headerVal = res.getHeader('content-type');
let allHeaders = res.getHeaders();
let size = res.getSize(); // { header: 234, body: 1024, total: 1258 }

// Modifying the response body for downstream scripts
res.setBody({ message: "Modified response" });
```

36 changes: 24 additions & 12 deletions testing/script/response/response-query.mdx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
---
title: "Response Query"
title: "Response query"
description: "Query and filter nested response data using dot notation, deep navigation, and array filtering"
---

The `res` object that is available inside the [vars](../vars), [assertions](../../tests/assertions), [scripting](../getting-started) and [testing](../../tests/introduction) contexts can be queried for data by invoking it like below.
The `res` object available inside the [vars](../vars), [assertions](../../tests/assertions), [scripting](../getting-started), and [testing](../../tests/introduction) contexts can be invoked as a function to query nested response data.

Think of it as `lodash.get()` on steroids
Think of it as `lodash.get()` on steroids.

```javascript
res('order.total')
```

<Info>
Response query filtering (including `[?]` with filter functions and object predicates) works in both **Safe Mode** and **Developer Mode**.
</Info>

## Examples
```javascript
const data = {
Expand Down Expand Up @@ -38,10 +43,10 @@
```
| Query | Output |
|-----------------------------------------|------------------------------------|
| res("customer.address.city") | bangalore |

Check warning on line 46 in testing/script/response/response-query.mdx

View check run for this annotation

Mintlify / Mintlify Validation (bruno-a6972042) - vale-spellcheck

testing/script/response/response-query.mdx#L46

Did you really mean 'bangalore'?
| res("customer.orders.items.amount") | [10, 20, 30, 40] |
| res("customer.orders.items.amount[0]") | 10 |
| res("..items.amount") | [10, 20, 30, 40] | |
| res("..items.amount") | [10, 20, 30, 40] |
| res("..amount") | [10, 20, 30, 40] |
| res("..items.amount[0]") | 10 |
| res("..items[0].amount") | 10 |
Expand All @@ -56,35 +61,42 @@

### Standard dot notation

**Example:**
```javascript
res('customer.orders.items.amount')
```

### Deep navigation .. double dots
### Deep navigation with double dots

**Example:**
```javascript
res('..items.amount')
```

### Array indexing

**Example:**
```javascript
res('..items[0].amount')
```

### Array filtering [?] with corresponding filter function
### Array filtering with a filter function

Use `[?]` with a corresponding filter function to select matching items.

**Example:**
```javascript
res('..items[?].amount', i => i.amount > 20)
```

### Array mapping [?] with corresponding mapper function
### Array filtering with an object predicate

Use `[?]` with an object to match items by property values. This is equivalent to `i => i.id === 2 && i.amount === 20`.

```javascript
res('..items[?]', { id: 2, amount: 20 })
```

### Array mapping with a mapper function

Use `[?]` with a function that returns a transformed value to map over items.

**Example:**
```javascript
res('..items..amount[?]', amt => amt + 10)
```
Expand Down
Loading