Class: Request

Represents an HTTP request.

Methods

setEntity

Sets the provided Entity to the request.

getQueryParams

Gets the query parameters of the request as a map consisting of a string array.

getQueryParamValue

Gets the query param value associated with the given key.

getQueryParamValues

Gets all the query param values associated with the given key.

getMatrixParams

Gets the matrix parameters of the request.

getEntity

Gets the Entity associated with the request.

hasHeader

Checks whether the requested header key exists in the header map.

getHeader

Returns the value of the specified header.

getHeaders

Gets all the header values to which the specified header key maps to.

setHeader

Sets the specified header to the request.

addHeader

Adds the specified header to the request.

removeHeader

Removes the specified header from the request.

removeAllHeaders

Removes all the headers from the request.

getHeaderNames

Gets all the names of the headers of the request.

expects100Continue

Checks whether the client expects a 100-continue response.

setContentType

Sets the content-type header to the request.

getContentType

Gets the type of the payload of the request (i.e: the content-type header value).

getJsonPayload

Extracts json payload from the request.

getXmlPayload

Extracts xml payload from the request.

getTextPayload

Extracts text payload from the request.

getByteStream

Gets the request payload as a stream of byte[], except in the case of multiparts.

getBinaryPayload

Gets the request payload as a byte[].

getFormParams

Gets the form parameters from the HTTP request as a map when content type is application/x-www-form-urlencoded.

getBodyParts

Extracts body parts from the request.

setJsonPayload

Sets a json as the payload.

setXmlPayload

Sets an xml as the payload.

setTextPayload

Sets a string as the payload.

setBinaryPayload

Sets a byte[] as the payload.

setBodyParts

Set multiparts as the payload.

setFileAsPayload

Sets the content of the specified file as the entity body of the request.

setByteStream

Sets a Stream as the payload.

setPayload

Sets the request payload.

addCookies

Adds cookies to the request.

getCookies

Gets cookies from the request.

Fields

  • rawPath string(default "")
  • Resource path of the request URL

  • method string(default "")
  • The HTTP request method

  • httpVersion string(default "")
  • The HTTP version supported by the client

  • userAgent string(default "")
  • The user-agent. This value is used when setting the user-agent header

  • extraPathInfo string(default "")
  • The part of the URL, which matched to '*' if the request is dispatched to a wildcard resource

  • cacheControl RequestCacheControl?(default ())
  • The cache-control directives for the request. This needs to be explicitly initialized if intending on utilizing HTTP caching.

  • mutualSslHandshake MutualSslHandshake?(default ())
  • A record providing mutual ssl handshake results.

setEntity

function setEntity(Entity e)
Isolated Function

Sets the provided Entity to the request.

Parameters

  • e Entity
  • The Entity to be set to the request

getQueryParams

function getQueryParams() returns map<string[ ]>
Isolated Function

Gets the query parameters of the request as a map consisting of a string array.

Return Type

(map<string[ ]>)

String array map of the query params

getQueryParamValue

function getQueryParamValue(string key) returns string?
Isolated Function

Gets the query param value associated with the given key.

Parameters

  • key string
  • Represents the query param key

Return Type

(string?)

The query param value associated with the given key as a string. If multiple param values are present, then the first value is returned. () is returned if no key is found.

getQueryParamValues

function getQueryParamValues(string key) returns string[ ]?
Isolated Function

Gets all the query param values associated with the given key.

Parameters

  • key string
  • Represents the query param key

Return Type

(string[ ]?)

All the query param values associated with the given key as a string[]. () is returned if no key is found.

getMatrixParams

function getMatrixParams(string path) returns map<any>
Isolated Function

Gets the matrix parameters of the request.

Parameters

  • path string
  • Path to the location of matrix parameters

Return Type

(map<any>)

A map of matrix parameters which can be found for the given path

getEntity

function getEntity() returns Entity | ClientError
Isolated Function

Gets the Entity associated with the request.

Return Type

(Entity | ClientError)

The Entity of the request. An http:ClientError is returned, if entity construction fails

hasHeader

function hasHeader(string headerName) returns boolean
Isolated Function

Checks whether the requested header key exists in the header map.

Parameters

  • headerName string
  • The header name

Return Type

(boolean)

true if the specified header key exists

getHeader

function getHeader(string headerName) returns string | HeaderNotFoundError
Isolated Function

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

Return Type

(string | HeaderNotFoundError)

The first header value for the specified header name or the HeaderNotFoundError if the header is not found.

getHeaders

function getHeaders(string headerName) returns string[ ] | HeaderNotFoundError
Isolated Function

Gets all the header values to which the specified header key maps to.

Parameters

  • headerName string
  • The header name

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)
Isolated Function

Sets the specified header to the request. 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

addHeader

function addHeader(string headerName, string headerValue)
Isolated Function

Adds the specified header to the request. Existing header values are not replaced. Panic if an illegal header is passed.

Parameters

  • headerName string
  • The header name

  • headerValue string
  • The header value

removeHeader

function removeHeader(string headerName)
Isolated Function

Removes the specified header from the request.

Parameters

  • headerName string
  • The header name

removeAllHeaders

function removeAllHeaders()
Isolated Function

Removes all the headers from the request.

getHeaderNames

function getHeaderNames() returns string[ ]
Isolated Function

Gets all the names of the headers of the request.

Return Type

(string[ ])

An array of all the header names

expects100Continue

function expects100Continue() returns boolean
Isolated Function

Checks whether the client expects a 100-continue response.

Return Type

(boolean)

true if the client expects a 100-continue response

setContentType

function setContentType(string contentType) returns error?
Isolated Function

Sets the content-type header to the request.

Parameters

  • contentType string
  • Content type value to be set as the content-type header

Return Type

(error?)

Nil if successful, error in case of invalid content-type

getContentType

function getContentType() returns string
Isolated Function

Gets the type of the payload of the request (i.e: the content-type header value).

Return Type

(string)

The content-type header value as a string

getJsonPayload

function getJsonPayload() returns json | ClientError
Isolated Function

Extracts json payload from the request. If the content type is not JSON, an http:ClientError is returned.

Return Type

(json | ClientError)

The json payload or http:ClientError in case of errors

getXmlPayload

function getXmlPayload() returns xml | ClientError
Isolated Function

Extracts xml payload from the request. If the content type is not XML, an http:ClientError is returned.

Return Type

(xml | ClientError)

The xml payload or http:ClientError in case of errors

getTextPayload

function getTextPayload() returns string | ClientError
Isolated Function

Extracts text payload from the request. If the content type is not of type text, an http:ClientError is returned.

Return Type

(string | ClientError)

The text payload or http:ClientError in case of errors

getByteStream

function getByteStream(int arraySize) returns stream<byte[ ], Error?> | ClientError
Isolated Function

Gets the request payload as a stream of byte[], except in the case of multiparts. To retrieve multiparts, use Request.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
Isolated Function

Gets the request payload as a byte[].

Return Type

(byte[ ] | ClientError)

The byte[] representation of the message payload or http:ClientError in case of errors

getFormParams

function getFormParams() returns map<string> | ClientError
Isolated Function

Gets the form parameters from the HTTP request as a map when content type is application/x-www-form-urlencoded.

Return Type

(map<string> | ClientError)

The map of form params or http:ClientError in case of errors

getBodyParts

function getBodyParts() returns Entity[ ] | ClientError
Isolated Function

Extracts body parts from the request. 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 constructing the body parts from the request

setJsonPayload

function setJsonPayload(json payload, string contentType)
Isolated Function

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

setXmlPayload

function setXmlPayload(xml payload, string contentType)
Isolated Function

Sets an xml as the payload.

Parameters

  • payload xml
  • The xml payload

  • contentType string (default "application/xml")
  • The content type of the payload. Set this to override the default content-type header value for xml

setTextPayload

function setTextPayload(string payload, string contentType)
Isolated Function

Sets a string as the payload.

Parameters

  • payload string
  • The string payload

  • contentType string (default "text/plain")
  • The content type of the payload. Set this to override the default content-type header value for string

setBinaryPayload

function setBinaryPayload(byte[ ] payload, string contentType)
Isolated Function

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

function setBodyParts(Entity[ ] bodyParts, string contentType)
Isolated Function

Set multiparts as the payload.

Parameters

  • bodyParts Entity[ ]
  • The entities which make up the message body

  • contentType string (default "multipart/form-data")
  • The content type of the top level message. Set this to override the default content-type header value

setFileAsPayload

function setFileAsPayload(string filePath, string contentType)
Isolated Function

Sets the content of the specified file as the entity body of the request.

Parameters

  • filePath string
  • Path to the file to be set as the payload

  • contentType string (default "application/octet-stream")
  • The content type of the specified file. Set this to override the default content-type header value

setByteStream

function setByteStream(stream<byte[ ], Error?> byteStream, string contentType)
Isolated Function

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.

Parameters

  • byteStream stream<byte[ ], Error?>
  • Byte stream, which needs to be set to the request

  • contentType string (default "application/octet-stream")
  • Content-type to be used with the payload. This is an optional parameter. The application/octet-stream is the default value

setPayload

function setPayload(string | xml | json | byte[ ] | Entity[ ] | stream<byte[ ], Error?> payload)
Isolated Function

Sets the request payload. Note that any string value is set as text/plain. To send a JSON-compatible string, set the content-type header to application/json or use the setJsonPayload method instead.

Parameters

  • payload string | xml | json | byte[ ] | Entity[ ] | stream<byte[ ], Error?>
  • Payload can be of type string, xml, json, byte[], stream<byte[], io:Error?> or Entity[] (i.e., a set of body parts).

addCookies

function addCookies(Cookie[ ] cookiesToAdd)
Isolated Function

Adds cookies to the request.

Parameters

  • cookiesToAdd Cookie[ ]
  • Represents the cookies to be added

getCookies

function getCookies() returns Cookie[ ]
Isolated Function

Gets cookies from the request.

Return Type

(Cookie[ ])

An array of cookie objects, which are included in the request