rq.response object provides access to all details of the API response in your Requestly scripts. You can use these properties and methods primarily in post-response scripts to process, validate, and extract data from API responses.
Properties and Methods
rq.response.body
The body of the response as a string.
Example:
rq.response.bodyEncoding
Tells you how rq.response.body encodes the original response bytes:
'utf8'- the body is the decoded text. This is the case for JSON, XML, HTML, and other text responses.'base64'- the body is Base64-encoded raw bytes, used for binary responses such as images, PDFs, and archives.
'utf8'.
Example:
rq.response.code
The HTTP status code of the response, as a number.
Example:
rq.response.status
The status text of the response, for example "OK" or "Not Found". It is the text the server sent, or the standard text for the status code when the server sent none. Use rq.response.code when you want the number.
Example:
rq.response.statusText
The status text exactly as the server sent it. It is an empty string when the server sent none, which is always the case over HTTP/2.
rq.response.responseTime
The time taken by the request to complete, in milliseconds.
Example:
rq.response.headers
The response headers, as a list. Read a header with get(name), which matches the name ignoring case, or read it as a property using the name the server sent, such as rq.response.headers["Content-Type"]. The list also has these read methods, which work as they do on rq.request.headers:
has(name): whether a header with that name exists.one(name): the last header with that name, as an object withkeyandvalue.count(): the number of headers.all(): every header, as a list of objects withkeyandvalue.toObject(): the headers as a{ name: value }object. Names are lower-cased unless you passtoObject(false, true).each(fn),map(fn),filter(fn),find(fn)andreduce(fn, initial): loop over the headers.
JSON.stringify(rq.response.headers) returns the list of { key, value } objects.
Example:
rq.response.cookies
Using cookie objects in scripts requires the Requestly desktop app, version 2611.5.2 or later. Download it here.
rq.cookies. get(name) returns a cookie’s value, one(name) returns the cookie with its name, value, domain, path, secure, httpOnly and expires, and count(), has(name), all(), each(fn) and toObject() work as they do on the header list.
Example:
rq.response.id
A unique ID for this response object, generated when the script starts.
rq.response.responseSize
The size of the response body, in bytes.
rq.response.json()
Parses the response body as JSON and returns it as a JavaScript object. A byte order mark at the start of the body is ignored. Throws if the body is not valid JSON.
Example:
rq.response.jsonp()
Parses a JSONP response body, such as callback({"ok": 1});, by removing the function call around the JSON and parsing the rest. Throws if what remains is not valid JSON.
Example:
rq.response.text()
Returns the response body as a plain string.
Example:
rq.response.reason()
Returns the status text: the same value as rq.response.status. Returns undefined when the response has no status text and its code has no standard one.
rq.response.details()
Returns an object describing the status: name (the status text), code, standardName (the standard text for the code) and detail (a one-line explanation of the code). For a status code with no standard text, standardName and detail are left out, and name is set only when the server sent a status text.
Example:
rq.response.contentInfo()
Returns what the Content-Type and Content-Disposition headers say about the body:
contentType: the media type without parameters, for example"application/json". It is"text/plain"when the response has noContent-Type.mimeType: the kind of content, such as"text","image","audio","video"or"embed".mimeFormat: the format, such as"json","xml","html","plain"or"pdf".charset: the character set,"utf8"when the header names none.fileExtension: the usual file extension for the media type, when it has a common one.fileName: the file name fromContent-Disposition, orresponseplus the extension.
rq.response.dataURI()
Returns the body as a data: URI with the body Base64-encoded, for example "data:image/png;base64, iVBORw0KG...". Use it to embed a response, such as an image, in a visualization.
rq.response.size()
Returns the size of the response in bytes, as { body, header, total }. body comes from the Content-Length header when it has one, else from the body itself. header counts the status line and the headers.
Example:
rq.response.encoding()
Returns how the body is compressed, as { format, source }. When the response has a Content-Encoding header, format is its value and source is "header". A gzip body without that header gives format: "gzip" and source: "body". Otherwise both are undefined.
rq.response.update()
Changes the rq.response object in your script. Pass any of code, reason (the status text), header (a list of { key, value }), cookie, body and responseTime. A new code also updates status to the standard text for that code, unless you pass reason. The header and cookie lists are replaced by the ones you pass, and are empty when you pass none. The response shown in the app does not change.
Example:
rq.response.toJSON()
Returns the response as a plain object with these keys: status (the status code as a number), statusText, code (the status code), headers (a { name: value } object), header (a list of { key, value }), cookie (a list of the response’s cookies), body, bodyEncoding ("utf8" or "base64", when the response has one), time and responseTime (the response time), and id. JSON.stringify(rq.response) returns the same.
In this object
status is the numeric status code. The rq.response.status property itself is the status text ("OK"), and statusText holds it here.rq.response.mime()
Not supported: calling it throws an error. Use contentInfo() instead.
rq.response.to
Assertions about the response, for use inside rq.test(). rq.response.to is rq.expect(rq.response).to, so it takes every assertion rq.expect takes, plus the status, header, body, JSON, timing and schema assertions listed in Writing Tests.

