Class: Response
Represents an HTTP response.
Methods
getEntity | Gets the |
setEntity | Sets the provided |
hasHeader | Checks whether the requested header key exists in the header map. |
getHeader | Returns the value of the specified header. |
addHeader | Adds the specified header to the response. |
getHeaders | Gets all the header values to which the specified header key maps to. |
setHeader | Sets the specified header to the response. |
removeHeader | Removes the specified header from the response. |
removeAllHeaders | Removes all the headers from the response. |
getHeaderNames | Gets all the names of the headers of the response. |
setContentType | Sets the |
getContentType | Gets the type of the payload of the response (i.e., the |
getJsonPayload | Extract |
getXmlPayload | Extracts |
getTextPayload | Extracts |
getByteStream | Gets the response payload as a stream of byte[], except in the case of multiparts. |
getBinaryPayload | Gets the response payload as a |
getBodyParts | Extracts body parts from the response. |
setETag | Sets the |
setLastModified | Sets the current time as the |
setJsonPayload | Sets a |
setXmlPayload | Sets an |
setTextPayload | Sets a |
setBinaryPayload | Sets a |
setBodyParts | Set multiparts as the payload. |
setFileAsPayload | Sets the content of the specified file as the entity body of the response. |
setByteStream | Sets a |
setPayload | Sets the response payload. |
addCookie | Adds the cookie to response. |
removeCookiesFromRemoteStore | Deletes the cookies in the client's cookie store. |
getCookies | Gets cookies from the response. |
Fields
- statusCode int(default 200)
- reasonPhrase string(default "")
- server string(default "")
- resolvedRequestedURI string(default "")
- cacheControl ResponseCacheControl?(default ())
The response status code
The status code reason phrase
The server header
The ultimate request URI that was made to receive the response when redirect is on
The cache-control directives for the response. This needs to be explicitly initialized if intending on utilizing HTTP caching. For incoming responses, this will already be populated if the response was sent with cache-control directives
getEntity
function getEntity() returns Entity | ClientError
Gets the Entity
associated with the response.
Return Type
(Entity | ClientError)The Entity
of the response. An http:ClientError
is returned, if entity construction fails
hasHeader
function hasHeader(string headerName, HeaderPosition position) returns boolean
Checks whether the requested header key exists in the header map.
Parameters
- headerName string
The header name
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter
getHeader
function getHeader(string headerName, HeaderPosition position) returns string | HeaderNotFoundError
Returns the value of the specified header. If the specified header key maps to multiple values, the first of these values is returned.
Parameters
- headerName string
The header name
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
Return Type
(string | HeaderNotFoundError)The first header value for the specified header name or the HeaderNotFoundError
if the header is not
found.
addHeader
function addHeader(string headerName, string headerValue, HeaderPosition position)
Adds the specified header to the response. Existing header values are not replaced. Panic if an illegal header is passed.
Parameters
- headerName string
The header name
- headerValue string
The header value
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
getHeaders
function getHeaders(string headerName, HeaderPosition position) returns string[ ] | HeaderNotFoundError
Gets all the header values to which the specified header key maps to.
Parameters
- headerName string
The header name
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
Return Type
(string[ ] | HeaderNotFoundError)The header values the specified header key maps to or the HeaderNotFoundError
if the header is not
found.
setHeader
function setHeader(string headerName, string headerValue, HeaderPosition position)
Sets the specified header to the response. If a mapping already exists for the specified header key, the existing header value is replaced with the specified header value. Panic if an illegal header is passed.
Parameters
- headerName string
The header name
- headerValue string
The header value
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
removeHeader
function removeHeader(string headerName, HeaderPosition position)
Removes the specified header from the response.
Parameters
- headerName string
The header name
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
removeAllHeaders
function removeAllHeaders(HeaderPosition position)
Removes all the headers from the response.
Parameters
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
getHeaderNames
function getHeaderNames(HeaderPosition position) returns string[ ]
Gets all the names of the headers of the response.
Parameters
- position HeaderPosition (default LEADING)
Represents the position of the header as an optional parameter. If the position is http:TRAILING
,
the entity-body of the Response
must be accessed initially.
getJsonPayload
function getJsonPayload() returns json | ClientError
Extract json
payload from the response. If the content type is not JSON, an http:ClientError
is returned.
getXmlPayload
function getXmlPayload() returns xml | ClientError
Extracts xml
payload from the response.
getTextPayload
function getTextPayload() returns string | ClientError
Extracts text
payload from the response.
Return Type
(string | ClientError)The string representation of the message payload or http:ClientError
in case of errors
getByteStream
function getByteStream(int arraySize) returns stream<byte[ ], Error?> | ClientError
Gets the response payload as a stream of byte[], except in the case of multiparts. To retrieve multiparts, use
Response.getBodyParts()
.
Parameters
- arraySize int (default 8196)
A defaultable parameter to state the size of the byte array. Default size is 8KB
Return Type
(stream<byte[ ], Error?> | ClientError)A byte stream from which the message payload can be read or http:ClientError
in case of errors
getBinaryPayload
function getBinaryPayload() returns byte[ ] | ClientError
Gets the response payload as a byte[]
.
Return Type
(byte[ ] | ClientError)The byte[] representation of the message payload or http:ClientError
in case of errors
getBodyParts
function getBodyParts() returns Entity[ ] | ClientError
Extracts body parts from the response. If the content type is not a composite media type, an error is returned.
Return Type
(Entity[ ] | ClientError)The body parts as an array of entities or else an http:ClientError
if there were any errors in
constructing the body parts from the response
setLastModified
function setLastModified()
Sets the current time as the last-modified
header.
setJsonPayload
function setJsonPayload(json payload, string contentType)
Sets a json
as the payload.
Parameters
- payload json
The json
payload
- contentType string (default "application/json")
The content type of the payload. Set this to override the default content-type
header value
for json
setTextPayload
Sets a string
as the payload.
setBinaryPayload
function setBinaryPayload(byte[ ] payload, string contentType)
Sets a byte[]
as the payload.
Parameters
- payload byte[ ]
The byte[]
payload
- contentType string (default "application/octet-stream")
The content type of the payload. Set this to override the default content-type
header value
for byte[]
setBodyParts
Set multiparts as the payload.
setFileAsPayload
Sets the content of the specified file as the entity body of the response.
setByteStream
Sets a Stream
as the payload. This method overrides any existing content-type headers with the default
content-type, which is application/octet-stream
. This default value can be overridden by passing the
content-type as an optional parameter.
setPayload
Sets the response payload.