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.
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 anmime:InvalidContentTypeError
in case of invalid media-type
Gets the content type of the entity.
string contentType = mimeEntity.getContentType();
-
Return Type
(string) Content type as a
string
Sets the content ID of the entity.
mimeEntity.setContentId("test-id");
Parameters
- contentId string
-
Content ID, which needs to be set to the entity
Gets the content ID of the entity.
string contentId = mimeEntity.getContentId();
-
Return Type
(string) Content ID as a
string
Sets the content length of the entity.
mimeEntity.setContentLength(45555);
Parameters
- contentLength int
-
Content length, which needs to be set to the entity
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
Sets the content disposition of the entity.
mimeEntity.setContentDisposition(contentDisposition);
Parameters
- contentDisposition ContentDisposition
-
Content disposition, which needs to be set to the entity
Gets the content disposition of the entity.
-
Return Type
(ContentDisposition) A
ContentDisposition
object
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
, orEntity[]
.
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
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
Extracts the JSON body from the entity.
-
Return Type
(json | ParserError) json
data extracted from the entity body or else anmime:ParserError
if the entity body is not a JSON
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
Extracts the xml
body from the entity.
-
Return Type
(xml | ParserError) xml
data extracted from the entity body or else anmime:ParserError
if the entity body is not an XML
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
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 anmime:ParserError
if the entity body is not text compatible
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
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 amime:ParserError
in case of errors
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
- byteChannel ReadableByteChannel
-
Byte channel, 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
Gets the entity body as a byte channel from a given entity.
-
Return Type
(ReadableByteChannel | ParserError) An
io:ReadableByteChannel
or else amime:ParserError
record will be returned in case of errors
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 amime:ParserError
if the entity body is not a set of the body parts
Gets the body parts as a byte channel from a given entity.
-
Return Type
(ReadableByteChannel | ParserError) Body parts as a byte channel
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
.
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. UseEntity.hasHeader()
beforehand to check the existence of a header
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 theEntity
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 theEntity.hasHeader()
beforehand to check the existence of a header
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[]
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
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
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 theEntity
must be accessed initially.
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 theEntity
must be accessed initially.
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 theEntity
must be accessed initially.
-
Return Type
(boolean) true
if the specified header key exists