Skip to main content
The rq.request object provides access to all details of the API request in your Requestly scripts. You can use these properties and methods in both pre-request and post-response scripts to read data.

Properties and Methods

rq.request.method

Use this property to get the Request’s method. The HTTP method of the request (e.g. GET, POST, PUT, OPTION, DELETE, PATCH, HEAD). Example:
console.log("Request Method: ", rq.request.method);

rq.request.headers

An list of header rows each having key, value & isEnabled for each custom header you create. Example:
console.log("Headers: ", JSON.stringify(rq.request.headers));
You can also modify request headers from a pre-request script. The methods below change the headers that are sent with the request. Use them in pre-request scripts; in post-response scripts the request has already been sent, so header changes have no effect. Header changes apply to HTTP and GraphQL requests.

rq.request.headers.add(header)

Adds a header. If a header with the same name already exists, this adds a second one (it does not replace the existing header). Parameters:
  • header (object): { key, value } - the header name and value.
Example:
rq.request.headers.add({ key: "X-Trace-Id", value: "abc-123" });

rq.request.headers.upsert(header)

Adds a header, or replaces it if a header with the same name already exists. Header names are matched case-insensitively. Parameters:
  • header (object): { key, value } - the header name and value.
Example:
rq.request.headers.upsert({ key: "Authorization", value: "Bearer " + rq.environment.get("authToken") });

rq.request.headers.remove(name)

Removes every header with the given name (case-insensitive). Parameters:
  • name (string): The header name to remove.
Example:
rq.request.headers.remove("X-Debug");

rq.request.headers.clear()

Removes all request headers. Parameters: none. Any argument passed is ignored. clear() always removes every header, so to remove a single header use remove(name) instead. Example:
rq.request.headers.clear();
The same operations are available directly on rq.request as rq.request.addHeader({ key, value }), rq.request.upsertHeader({ key, value }), and rq.request.removeHeader(name).

rq.request.body

The body of the request, accessible as a string. If you have selected Form under Body it would be returned in object format. Example:
console.log("Body:", JSON.stringify(rq.request.body));

rq.request.url

The full URL of the API request. Example:
console.log("Request URL:", rq.request.url);

rq.request.queryParams

An object containing rows of query parameters each having key, value & isEnabled. Example:
console.log("Query Params:", JSON.stringify(rq.request.queryParams));

Common Use Cases

Logging Request Details

console.log("Making " + rq.request.method + " request to " + rq.request.url);
console.log("Request headers:", JSON.stringify(rq.request.headers));
console.log("Request body:", JSON.stringify(rq.request.body));

Conditional Logic Based on Request Method

if (rq.request.method === "POST" || rq.request.method === "PUT") {
    console.log("Sending data:", rq.request.body);
}

Accessing Query Parameters

rq.request.queryParams.forEach(param => {
    if (param.isEnabled) {
        console.log(`Query param ${param.key}: ${param.value}`);
    }
});

Adding an Authentication Header

// In a pre-request script
const token = rq.environment.get("authToken");
if (token) {
    rq.request.headers.upsert({ key: "Authorization", value: "Bearer " + token });
}