The request object

This object is always available as context.request in all HTTP filters. It contains a representation of the original client request.

Some properties can be changed in request filters, and such changes will affect the request as it is forwarded to the server. In response filters, it is pointless to change anything in the request object.

The request object has the following properties:

  • protocol (string, read-only): either "http" or "https"

  • address (string, read-only): the host name specified by the client in its request

  • port (number, read-only): the port number specified by the client in its request

  • method (string): the method of the request, one of: GET, POST, PUT, DELETE, OPTIONS, PATCH, TRACE, HEAD, CONNECT.

  • url (string): the URL requested by the client, without the protocol, address or port, e.g. /api/customers/123?c=US

  • destinationProtocol (string): if this is set to either "http" or "https", this request will be forwarded using that protocol instead of the one specified in the connection settings.

  • destinationHost (string): if set, this request will be forwarded to that host rather than the one specified in the connection settings.

  • destinationPort (number): if set, this request will be forwarded to that port rather than the one specified in the connection settings.

  • destinationMethod (string): if set, this method will be used when forwarding the request, instead of the original request's method.

  • destinationUrl (string): if set, this request will be forwarded to that URL (e.g. /api/customers/123?c=US) instead of the original request's URL.

  • destinationFullUrl (string): if set, this request will be forwarded to that full URL (e.g. https://rest.acme.com:8443/api/customers/123?c=US). This overrides other destination settings.

  • clientAddress (object , read-only): the address of the client as a Java InetAddress object (either an Inet4Address or an Inet6Address).

  • clientAddressString (string, read-only): the address of the client as a string. Can be either an IP4 address or an IP6 address.

  • payload (byte array): the body of the response, if present

  • payloadString (string): the body of the response (if present) as a UTF-8 string. If the body is not a string, this property should not be accessed.

  • payloadStream (object, read-only): returns a Java InputStream that can be used to read the content of the payload.

  • payloadWriteStream (object, read-only): returns a Java OutputStream that can be used to write to the payload.

In addition, the request object has the following methods:

  • string getHeader(string name): returns the value of the specified HTTP header, if present. The name is case-insensitive.

  • boolean hasHeader(string name): returns true if the specified header is set.

  • void setHeader(string name, string value): sets the value of the specified HTTP header. Setting the value to null removes the header.

  • string getPayloadString(string enc): gets the request's payload as a string decoded using the specified encoding, e.g. UTF-16 or ISO-8859-1.

  • void setPayloadString(string value, string enc): sets the request's payload to the given string, encoding it according to the provided encoding, e.g. UTF-16 or ISO-8859-1.

Examples

Change the URL to which a request will be forwarded

let url = context.request.url;

if (url.endsWith("?format=xml")) {

context.request.destinationUrl = url.substring(0, url.length - 3) + "json";

}


Change the body of a request

let stmt = context.request.payloadString;

if (/\s*show\s+catalogs\s*/.test(stmt)) {

context.request.payloadString = "show catalogs like 't%'";

}

else if (/\s*show\s+schemas\s*/.test(stmt)) {

context.request.payloadString = "show schemas like 'a%'";

}


Change a header in a request

if (context.request.hasHeader("Date")) {

log.trace("Setting Date header");

context.request.setHeader("Date", "Beginning of time");

}