RFC 2045 standard. "> API Docs - Object : Entity

Object - mime : 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

Sets the content-type to the entity.
Gets the content type of the entity.
Sets the content ID of the entity.
Gets the content ID of the entity.
Sets the content length of the entity.
Gets the content length of the entity.
Sets the content disposition of the entity.
Gets the content disposition of the entity.
Sets the body of the entity with the given content.
Sets the entity body with a given file.
Sets the entity body with the given json content.
Extracts the JSON body from the entity.
Sets the entity body with the given XML content.
Extracts the xml body from the entity.
Sets the entity body with the given text content.
Extracts the text body from the entity.
Sets the entity body with the given byte[] content.
Gets the entity body as a byte[] from a given entity.
Sets the entity body with the given byte channel content.
Gets the entity body as a byte channel from a given entity.
Gets the body parts from a given entity.
Gets the body parts as a byte channel from a given entity.
Sets body parts to entity.
Gets the header value associated with the given header name.
Gets all the header values associated with the given header name.
Gets all the header names.
Adds the given header value against the given header.
Sets the given header value against the existing header.
Removes the given header from the entity.
Removes all headers associated with the entity.
Checks whether the requested header key exists in the header map.

setContentType

(string mediaType)

returns InvalidContentTypeError?

Sets the content-type to the entity.

 mime: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

()

returns string

Gets the content type of the entity.

 string contentType = mimeEntity.getContentType();

  • Return Type

    (string)
  • Content type as a string

setContentId

Sets the content ID of the entity.

 mimeEntity.setContentId("test-id");

Parameters

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

getContentId

()

returns string

Gets the content ID of the entity.

 string contentId = mimeEntity.getContentId();

  • Return Type

    (string)
  • Content ID as a string

setContentLength

Sets the content length of the entity.

 mimeEntity.setContentLength(45555);

Parameters

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

getContentLength

()

returns int | error

Gets the content length of the entity.

 int|error contentLength = mimeEntity.getContentLength();

  • Return Type

    (int | error)
  • Content length as an int or else an error in case of a failure

setContentDisposition

Sets the content disposition of the entity.

 mimeEntity.setContentDisposition(contentDisposition);

Parameters

  • contentDisposition ContentDisposition
  • Content disposition, which needs to be set to the entity

getContentDisposition

()

returns ContentDisposition

Gets the content disposition of the entity.

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.

 mimeEntity.setBody("body string");

Parameters

  • entityBody string | xml | json | byte[] | ReadableByteChannel | Entity[]
  • Entity body can be of the type string,xml,json,byte[],io:ReadableByteChannel, or Entity[].

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.

 mimeEntity.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

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.

 mimeEntity.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

()

returns json | ParserError

Extracts the JSON body from the entity.

  • 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.

 mimeEntity.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

()

returns xml | ParserError

Extracts the xml body from the entity.

  • 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.

 mimeEntity.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

()

returns string | ParserError

Extracts the text body from the entity. If the entity body is not text compatible, an error is returned.

  • 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

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.

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

()

returns byte[] | ParserError

Gets the entity body as a byte[] from a given entity. If the entity size is considerably large, consider using the getByteChannel() method instead.

  • Return Type

    (byte[] | ParserError)
  • byte[] data extracted from the the entity body or else a mime:ParserError in case of errors

setByteChannel

Sets the entity body with the given byte channel 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.

Parameters

  • 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

getByteChannel

()

returns ReadableByteChannel | ParserError

Gets the entity body as a byte channel from a given entity.

getBodyParts

()

returns Entity[] | ParserError

Gets the body parts from a given entity.

  • 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

getBodyPartsAsChannel

()

returns ReadableByteChannel | ParserError

Gets the body parts as a byte channel from a given entity.

setBodyParts

Sets body parts to 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.

Parameters

  • bodyParts Entity[]
  • Body parts, which needs 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

(string headerName, HeaderPosition position)

returns string

Gets the header value associated with the given header name.

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

Parameters

  • headerName string
  • Header name

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter

  • Return Type

    (string)
  • Header value associated with the given header name as a string. If multiple header values are present, then the first value is returned. An exception is thrown if no header is found. Use Entity.hasHeader() beforehand to check the existence of a header

getHeaders

(string headerName, HeaderPosition position)

returns string[]

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

Parameters

  • headerName string
  • Header name

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter. If the position is mime:TRAILING, the body of the Entity must be accessed initially

  • Return Type

    (string[])
  • All the header values associated with the given header name as a string[]. Panics if no header is found. Use the Entity.hasHeader() beforehand to check the existence of a header

getHeaderNames

(HeaderPosition position)

returns string[]

Gets all the header names.

 string[] headerNames = mimeEntity.getHeaderNames();

Parameters

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter

  • Return Type

    (string[])
  • All header names as a string[]

addHeader

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

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

Parameters

  • headerName string
  • Header name

  • headerValue string
  • The header value to be added

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter

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.

 mimeEntity.setHeader("custom-header", "header-value");

Parameters

  • headerName string
  • Header name

  • headerValue string
  • Header value

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter

removeHeader

Removes the given header from the entity.

Parameters

  • headerName string
  • Header name

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter. If the position is mime:TRAILING, the body of the Entity must be accessed initially.

removeAllHeaders

Removes all headers associated with the entity.

Parameters

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter. If the position is mime:TRAILING, the body of the Entity must be accessed initially.

hasHeader

(string headerName, HeaderPosition position)

returns boolean

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

Parameters

  • headerName string
  • Header name

  • position HeaderPosition (default LEADING)
  • Position of the header as an optional parameter. If the position is mime:TRAILING, the body of the Entity must be accessed initially.

  • Return Type

    (boolean)
  • true if the specified header key exists