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 site.
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 |
---|---|
void add(String key, String val) | Adds HTTP header
fields for the request. Info:
If Groovy Rules are used to inject HTTP headers for the backend protected application, the script must sanitize the same headers from the original client request. |
String getAccept() | Returns the acceptable response Content-Types expected by the User-Agent. |
void setAccept(String value) | Sets the acceptable response Content-Types expected by the User-Agent. |
String getAuthorization() | Returns the authentication credentials for HTTP Authentication. |
void setAuthorization(String username, String password) | Sets authentication credentials for HTTP Authentication. |
String getConnection() | Returns the connection type preferred by the User-Agent. |
void setConnection(List<String> values) | Sets the connection type preferred by the User-Agent. |
int getContentLength() | Returns the request body content length. |
void setContentLength(int length) | Sets the request body content length. |
MediaType getContentType() | Returns media type of Header with content type |
void setContentType(String) | Sets the request body MIME type. |
Map <String, String[]> getCookies() | Returns all cookies sent with the request. |
void setCookie(String) | Overwrites the request's cookie header with the passed string. This method cannot be used to set cookies in the response header. |
String getFirstCookieValue(String) | Returns the first cookie in the cookie header. |
String getFirstValue(String) | Returns the first value of the HTTP header specified by the name. |
void setDate(Date date) | Sets the date of the message in the Date HTTP header. |
List<GroovyHeaderField> getAllHeaderFields() | Returns a list of GroovyHeaderFields. |
String getHost() | Returns the hostname specified in the request. |
void setHost(String value) | Sets the hostname for the request to the Site. |
String getLocation() | Gets the redirect location URL for the response. |
void setLocation(String value) | Sets the redirect location URL for the response. |
String getProxyAuthorization() | Returns the proxy credentials. |
void setProxyAuthorization(String value) | Sets the request proxy credentials. |
void setServer(String value) | Sets the server name for the response. |
List<String> getValues(String name) | Returns a list of string values for the supplied header name. |
String getXForwardedFor() | Returns the originating IP address of the client and the proxies, if set. |
void setXForwardedFor(String value) | Sets the IP address for the client and the proxies. |
boolean removeContentEncoding() | Removes the Content-Encoding header value. Returns true if the value has been removed. |
boolean removeContentLength() | Removes the Content-Length header value. Returns true if the value has been removed. |
boolean removeContentType() | Removes the Content-Type header value. Returns true if the value has been removed. |
boolean removeExpect() | Removes the Expect header value. Returns true if the value has been removed. |
boolean removeFields(String name) | Removes the header value specified by the name parameter. Returns true if the value has been removed. |
boolean removeTransferEncoding() | Removes the Transfer-Encoding header value. Returns true if the value has been removed. |
GroovyHeaderField object
Method | Description |
---|---|
String getValue(); | Returns the string's value. |
GroovyHeaderName getHeaderName(); | Returns the header's name. |
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()