Headers object reference
Access the Headers object in Groovy exc?.request?.header
or exc?.response?.header
.
Purpose
The Headers object contains the HTTP header information from the request made to an application or the HTTP header from a site response. The Request HTTP header is sent on to the site after the rules are evaluated. The Response HTTP header is returned to the client after the Response rules are evaluated.
Use the Headers object to add custom HTTP headers for a site, as demonstrated in the following example:
Example Groovy sample
if ( !(exc.response) ) { // Set a custom header for the Site request def header = exc?.request?.header header?.add("X-PINGACCESS-SAMPLE", "SUCCESS") } pass()
Method summary
Method | Description | ||
---|---|---|---|
|
Adds HTTP header fields for the request.
|
||
|
Returns the acceptable response Content-Types expected by the User-Agent. |
||
|
Sets the acceptable response Content-Types expected by the User-Agent. |
||
|
Returns the authentication credentials for HTTP authentication. |
||
|
Sets authentication credentials for HTTP authentication. |
||
|
Returns the connection type preferred by the User-Agent. |
||
|
Sets the connection type preferred by the User-Agent. |
||
|
Returns the request body content length. |
||
|
Sets the request body content length. |
||
|
Returns media type of Header with content type |
||
|
Sets the request body MIME type. |
||
|
Returns all cookies sent with the request. |
||
|
Overwrites the request’s cookie header with the passed string. This method cannot be used to set cookies in the response header. |
||
|
Returns the first cookie in the cookie header. |
||
|
Returns the first value of the HTTP header specified by the name. |
||
|
Sets the date of the message in the Date HTTP header. |
||
|
Returns a list of GroovyHeaderFields. |
||
|
Returns the host name specified in the request. |
||
|
Sets the host name for the request to the Site. |
||
|
Gets the redirect location Uniform Resource Locator (URL) for the response. |
||
|
Sets the redirect location URL for the response. |
||
|
Returns the proxy credentials. |
||
|
Sets the request proxy credentials. |
||
|
Sets the server name for the response. |
||
|
Returns a list of string values for the supplied header name. |
||
|
Returns the originating Internet Protocol (IP) address of the client and the proxies, if set. |
||
|
Sets the IP address for the client and the proxies. |
||
|
Removes the Content-Encoding header value. Returns true if the value has been removed. |
||
|
Removes the Content-Length header value. Returns true if the value has been removed. |
||
|
Removes the Content-Type header value. Returns true if the value has been removed. |
||
|
Removes the Expect header value. Returns true if the value has been removed. |
||
|
Removes the header value specified by the name parameter. Returns true if the value has been removed. |
||
|
Removes the Transfer-Encoding header value. Returns true if the value has been removed. |
GroovyHeaderField object
Method | Description |
---|---|
|
Returns the string’s value. |
|
Returns the header’s name. |
Example Groovy sample
The following example demonstrates usage of the getAllHeaderFields()
method, which includes both request and response logging:
exc?.log.info "Display Headers: " exc?.log.info "-->Request Headers" reqHdrs = exc?.request?.header?.getAllHeaderFields() reqLoop = reqHdrs?.iterator() while (reqLoop?.hasNext()) { hdr = reqLoop?.next() exc.log.info "-->reqHeader Name: "+hdr?.getHeaderName()?.toString() exc.log.info "-->reqHeader Value: "+ hdr?.getValue() } exc?.log.info "-->Response Headers" exc?.log.debug "-->Response HTTP Status: "+ exc?.response?.statusCode rspHdrs = exc?.response?.header?.getAllHeaderFields() rspLoop = rspHdrs?.iterator() while (rspLoop?.hasNext()) { hdr = rspLoop?.next () exc.log.info "-->rspHeader Name: "+ hdr?.getHeaderName()?.toString() exc.log.info "-->rspHeader Value: "+ hdr?.getValue() } exc?.log.info "Display Headers EOF: " pass()