The response object

In response filters, this object is available as context.response. It contains a representation of the response from the HTTP server, and has the following properties:

  • request (object): the request object, also obtainable as context.request

  • responseCode (int): the HTTP status code from the server. Usually 200 indicates success, but there are many other codes.

  • responseMessage (string): the HTTP response message from the server, usually something like "OK" or "Not Found"

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

  • bodyString (string): the body of the response (if present) as a UTF-8 string

  • 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 response object has the following methods:

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

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

  • void setResponseHeader(string name, string value): sets the value of the specified HTTP 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 a header in the response

if (context.response.hasResponseHeader("Date")) {

log.trace("Setting Date header");

context.response.setResponseHeader("Date", "Beginning of time");

}


Change the body of a response

Change the URLs coming back from the server to point to the Gallium Data proxy, and not the actual HTTP server.

context.response.payloadString =

context.response.payloadString.replaceAll("https://api.acme.com.dev", "http://mypc:8096");