Class: Entity
Represents the headers and body of a message. This can be used to represent both the entity of a top level message and an entity(body part) inside of a multipart entity.
Methods
setContentType | Sets the content-type to the entity. |
getContentType | Gets the content type of the entity. |
setContentId | Sets the content ID of the entity. |
getContentId | Gets the content ID of the entity. |
setContentLength | Sets the content length of the entity. |
getContentLength | Gets the content length of the entity. |
setContentDisposition | Sets the content disposition of the entity. |
getContentDisposition | Gets the content disposition of the entity. |
setBody | Sets the body of the entity with the given content. |
setFileAsEntityBody | Sets the entity body with a given file. |
setJson | Sets the entity body with the given |
getJson | Extracts the JSON body from the entity. |
setXml | Sets the entity body with the given XML content. |
getXml | Extracts the |
setText | Sets the entity body with the given text content. |
getText | Extracts the text body from the entity. |
setByteArray | Sets the entity body with the given byte[] content. |
getByteArray | Gets the entity body as a |
setByteStream | Sets the entity body with the given byte stream content. |
getByteStream | Gets the entity body as a stream of |
getBodyParts | Gets the body parts from a given entity. |
getBodyPartsAsStream | Gets the body parts as a byte stream from a given entity. |
setBodyParts | Sets the body parts to the entity. |
getHeader | Gets the header value associated with the given header name. |
getHeaders | Gets all the header values associated with the given header name. |
getHeaderNames | Gets all the header names. |
addHeader | Adds the given header value against the given header. |
setHeader | Sets the given header value against the existing header. |
removeHeader | Removes the given header from the entity. |
removeAllHeaders | Removes all headers associated with the entity. |
hasHeader | Checks whether the requested header key exists in the header map. |
setContentType
function setContentType(string mediaType) returns InvalidContentTypeError?
Sets the content-type to the entity.
1mime:InvalidContentTypeError? contentType = mimeEntity.setContentType("application/json");
Parameters
- mediaType string
Content type, which needs to be set to the entity
Return Type
(InvalidContentTypeError?)()
if successful or else an mime:InvalidContentTypeError
in case of invalid media-type
setContentDisposition
function setContentDisposition(ContentDisposition contentDisposition)
Sets the content disposition of the entity.
1mimeEntity.setContentDisposition(contentDisposition);
Parameters
- contentDisposition ContentDisposition
Content disposition, which needs to be set to the entity
getContentDisposition
function getContentDisposition() returns ContentDisposition
Gets the content disposition of the entity.
1mime:ContentDisposition contentDisposition = mimeEntity.getContentDisposition();
setBody
Sets the body of the entity with the given content. 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.
1mimeEntity.setBody("body string");
setFileAsEntityBody
Sets the entity body with a given file. 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.
1mimeEntity.setFileAsEntityBody("<file path>");
setJson
function setJson(json jsonContent, string contentType)
Sets the entity body with the given json
content. This method overrides any existing content-type
headers
with the default content-type, which is application/json
. This default value can be overridden
by passing the content type as an optional parameter.
1mimeEntity.setJson({ "Hello": "World" });
Parameters
- jsonContent json
JSON content, which needs to be set to the entity
- contentType string (default "application/json")
Content type to be used with the payload. This is an optional parameter.
The default value is application/json
getJson
function getJson() returns json | ParserError
Extracts the JSON body from the entity.
1json|mime:ParserError result = entity.getJson();
Return Type
(json | ParserError)json
data extracted from the entity body or else an mime:ParserError
if the entity body is not a JSON
setXml
Sets the entity body with the given XML content. This method overrides any existing content-type headers
with the default content-type, which is application/xml
. This default value can be overridden
by passing the content-type as an optional parameter.
1mimeEntity.setXml(xml `<hello> world </hello>`);
getXml
function getXml() returns xml | ParserError
Extracts the xml
body from the entity.
1xml|mime:ParserError result = entity.getXml();
Return Type
(xml | ParserError)xml
data extracted from the entity body or else an mime:ParserError
if the entity body is not an XML
setText
Sets the entity body with the given text content. This method overrides any existing content-type headers
with the default content-type, which is text/plain
. This default value can be overridden
by passing the content type as an optional parameter.
1mimeEntity.setText("Hello World");
getText
function getText() returns string | ParserError
Extracts the text body from the entity. If the entity body is not text compatible, an error is returned.
1string|mime:ParserError result = entity.getText();
Return Type
(string | ParserError)string
data extracted from the the entity body or else an mime:ParserError
if the entity body is not
text compatible
setByteArray
function setByteArray(byte[ ] blobContent, string contentType)
Sets the entity body with the given byte[] content. 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.
1entity.setByteArray(content.toBytes());
Parameters
- blobContent byte[ ]
byte[]
content that needs to be set to the entity
- contentType string (default "application/octet-stream")
Content type to be used with the payload. This is an optional parameter.
The default value is application/octet-stream
getByteArray
function getByteArray() returns byte[ ] | ParserError
Gets the entity body as a byte[]
from a given entity. If the entity size is considerably large, consider
using the Entity.getByteStream()
method instead.
1byte[]|mime:ParserError result = entity.getByteArray();
Return Type
(byte[ ] | ParserError)byte[]
data extracted from the the entity body or else a mime:ParserError
in case of
errors
setByteStream
Sets the entity body with the given byte stream content. 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.
1entity.setByteStream(byteStream);
getByteStream
function getByteStream(int arraySize) returns stream<byte[ ], Error?> | ParserError
Gets the entity body as a stream of byte[]
from a given entity.
1stream<byte[], io:Error?>|mime:ParserError str = entity.getByteStream();
Parameters
- arraySize int (default 8192)
A defaultable parameter to state the size of the byte array. The default size is 8KB
Return Type
(stream<byte[ ], Error?> | ParserError)A byte stream from which the payload can be read or mime:ParserError
in case of errors
getBodyParts
function getBodyParts() returns Entity[ ] | ParserError
Gets the body parts from a given entity.
1mime:Entity[]|mime:ParserError result = multipartEntity.getBodyParts();
Return Type
(Entity[ ] | ParserError)An array of body parts(Entity[]
) extracted from the entity body or else a mime:ParserError
if the
entity body is not a set of the body parts
getBodyPartsAsStream
function getBodyPartsAsStream(int arraySize) returns stream<byte[ ], Error?> | ParserError
Gets the body parts as a byte stream from a given entity.
1stream<byte[], io:Error?>|mime:ParserError str = multipartEntity.getBodyPartsAsStream();
Parameters
- arraySize int (default 8192)
A defaultable paramerter to state the size of the byte array. Default size is 8KB
setBodyParts
Sets the body parts to the entity. This method overrides any existing content-type
headers
with the default multipart/form-data
content-type. The default multipart/form-data
value can be overridden
by passing the content type as an optional parameter.
1multipartEntity.setBodyParts(bodyParts, contentType);
getHeader
function getHeader(string headerName) returns string | HeaderNotFoundError
Gets the header value associated with the given header name.
1string|mime:HeaderNotFoundError headerName = mimeEntity.getHeader(mime:CONTENT_LENGTH);
Parameters
- headerName string
Header name
Return Type
(string | HeaderNotFoundError)Header value associated with the given header name as a string
. If multiple header values are
present, then the first value is returned. The HeaderNotFoundError
is returned if the header is not
found
getHeaders
function getHeaders(string headerName) returns string[ ] | HeaderNotFoundError
Gets all the header values associated with the given header name.
1string[]|mime:HeaderNotFoundError headerNames = mimeEntity.getHeaders(mime:CONTENT_TYPE);
Parameters
- headerName string
Header name
Return Type
(string[ ] | HeaderNotFoundError)All the header values associated with the given header name as a string[]
or the
HeaderNotFoundError
if the header is not found
addHeader
Adds the given header value against the given header. Panic if an illegal header is passed.
1mimeEntity.addHeader("custom-header", "header-value");
setHeader
Sets the given header value against the existing header. If a header already exists, its value is replaced with the given header value. Panic if an illegal header is passed.
1mimeEntity.setHeader("custom-header", "header-value");
removeAllHeaders
function removeAllHeaders()
Removes all headers associated with the entity.
1mimeEntity.removeAllHeaders();