HTTPResponse

Represents the data returned by the <td.httpRequest> tag.

The contents of the response body can be accessed using the body property. If expecting JSON-encoded content, HTML, or XML content, a parsed representation of the content can be accessed using the json , html , or xml properties. If there are errors parsing the content, they are available in the parseError property. For example:

 <@td.httpRequest url="https://example.com/data.json" var="response"/>
 <#if response.status == 200>
   <#if response.contentType?contains('/json')>
     <#assign responseStruct = response.json!''/>
   <#elseif response.contentType?contains('/html')>
     <#assign responseStruct = response.html!''/>
   <#elseif response.contentType?contains('/xml')>
     <#assign responseStruct = response.xml!''/>
   </#if>
   <#if response.parseError??>
     <b>ERROR: ${response.parseError.message?html}</b>
     <pre>${response.body?html}</pre>
   </#if>
 </#if>
 

All headers are available in the headers property. The keys for the header names are represented in all lower-case, as well as in whatever case was presented in the actual response.

Introduced in version 4.2.

Properties

Map headers
Accesses a multi-valued map containing all of the headers present in the response.
long duration
The amount of time, in milliseconds, the request took to execute.
String responseUrl
Shortcut for getHeader("location") .
String contentType
Shortcut for getHeader("content-type")
int status
The HTTP status code of the response.
String statusText
The status message of the response.
byte [] raw
The uninterpreted bytes of the entity.
String defaultCharset
The default charset for this entity, if any.
String defaultContentType
The default content type for this entity, if any.
String body
The entity body as a string.
String text
The entity body as a string.
Document xml
The body as a parsed XML DOM structure. If parsing fails, this property will be null and any exceptions can be read from the parseError property. The DOM structure can be interrogated using normal Freemarker XML handling; see the Freemarker documentation ( https://freemarker.apache.org/docs/xgui.html for details.
Object json
The body as a parsed JSON object. This can also be done manually in Freemarker using response.body?eval ; this property is provided as a convenience. If the parsing of the JSON fails, this property will be null and any exceptions can be read from the parseError property.
Document html
The body as a DOM structure generated by the Jsoup Java library ( https://jsoup.org/ ). If the parsing of the body fails, this property will be null and any exceptions can be read from the parseError property.
String base64
The raw entity encoded as base64 string. This can be used to handle binary data, for example to store a file that includes binary data.
String dataUrl
The bytes encoded as a data URL using the appropriate default MIME type.
Exception parseError
The Java exception caused by an attempt to parse the body content when accessed via xml , html , or json .

Methods

String[] getHeaderValues( String name)
Retrieves all values for the header with the given name.
String getHeader( String name)
Utility method for retrieving the first value of a header.