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 json content.

getJson

Extracts the JSON body from the entity.

setXml

Sets the entity body with the given XML content.

getXml

Extracts the xml body from the entity.

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 byte[] from a given entity.

setByteStream

Sets the entity body with the given byte stream content.

getByteStream

Gets the entity body as a stream of byte[] from a given entity.

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

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

getContentType

function getContentType() returns string
Isolated Function

Gets the content type of the entity.

1string contentType = mimeEntity.getContentType();

Return Type

(string)

Content type as a string

setContentId

function setContentId(string contentId)
Isolated Function

Sets the content ID of the entity.

1mimeEntity.setContentId("test-id");

Parameters

  • contentId string
  • Content ID, which needs to be set to the entity

getContentId

function getContentId() returns string
Isolated Function

Gets the content ID of the entity.

1string contentId = mimeEntity.getContentId();

Return Type

(string)

Content ID as a string

setContentLength

function setContentLength(int contentLength)
Isolated Function

Sets the content length of the entity.

1mimeEntity.setContentLength(45555);

Parameters

  • contentLength int
  • Content length, which needs to be set to the entity

getContentLength

function getContentLength() returns int | error
Isolated Function

Gets the content length of the entity.

1int|error contentLength = mimeEntity.getContentLength();

Return Type

(int | error)

Content length as an int or else an error in case of a failure

setContentDisposition

function setContentDisposition(ContentDisposition contentDisposition)
Isolated Function

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

Gets the content disposition of the entity.

1mime:ContentDisposition contentDisposition = mimeEntity.getContentDisposition();

Return Type

(ContentDisposition)

A ContentDisposition object

setBody

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

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");

Parameters

  • entityBody string | xml | json | byte[ ] | Entity[ ] | stream<byte[ ], Error?>
  • Entity body can be of the type string,xml,json,byte[],Entity[], or stream<byte[], io:Error?>

setFileAsEntityBody

function setFileAsEntityBody(string filePath, string contentType)
Isolated Function

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>");

Parameters

  • filePath string
  • Path of the file

  • 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

setJson

function setJson(json jsonContent, string contentType)
Isolated Function

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

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

function setXml(xml xmlContent, string contentType)
Isolated Function

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>`);

Parameters

  • xmlContent xml
  • XML content, which needs to be set to the entity

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

getXml

function getXml() returns xml | ParserError
Isolated Function

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

function setText(string textContent, string contentType)
Isolated Function

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");

Parameters

  • textContent string
  • Text content, which needs to be set to the entity

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

getText

function getText() returns string | ParserError
Isolated Function

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

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

Gets the entity body as a byte[] from a given entity. If the entity size is considerably large, consider using the 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

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

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);

Parameters

  • byteStream stream<byte[ ], Error?>
  • Byte stream, which 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 application/octet-stream is the default value

getByteStream

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

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

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

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 8196)
  • A defaultable paramerter to state the size of the byte array. Default size is 8KB

Return Type

(stream<byte[ ], Error?> | ParserError)

Body parts as a byte stream

setBodyParts

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

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);

Parameters

  • bodyParts Entity[ ]
  • Body parts, which need to be set to the entity

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

getHeader

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

Gets the header value associated with the given header name.

1string|mime:HeaderNotFoundError headerName = mimeEntity.getHeader(mime:CONTENT_LENGTH);

Parameters

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

Gets all the header values associated with the given header name.

1string[]|mime:HeaderNotFoundError headerNames = mimeEntity.getHeaders(mime:CONTENT_TYPE);

Parameters

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

getHeaderNames

function getHeaderNames() returns string[ ]
Isolated Function

Gets all the header names.

1string[] headerNames = mimeEntity.getHeaderNames();

Return Type

(string[ ])

All header names as a string[]

addHeader

function addHeader(string headerName, string headerValue)
Isolated Function

Adds the given header value against the given header. Panic if an illegal header is passed.

1mimeEntity.addHeader("custom-header", "header-value");

Parameters

  • headerValue string
  • The header value to be added

setHeader

function setHeader(string headerName, string headerValue)
Isolated Function

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");

Parameters

  • headerValue string
  • Header value

removeHeader

function removeHeader(string headerName)
Isolated Function

Removes the given header from the entity.

1mimeEntity.removeHeader("custom-header");

Parameters

removeAllHeaders

function removeAllHeaders()
Isolated Function

Removes all headers associated with the entity.

1mimeEntity.removeAllHeaders();

hasHeader

function hasHeader(string headerName) returns boolean
Isolated Function

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

1boolean res = mimeEntity.hasHeader("custom-header");

Parameters

Return Type

(boolean)

true if the specified header key exists