This module contains helper functions for working with MIME type.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::module::Mime
to the header of your
DataWeave script.
Functions
fromString
fromString(mimeType: String): Result<MimeType, MimeTypeError>
Transforms a MIME type string value representation to a MimeType
.
Parameters
Name | Type | Description |
---|---|---|
mimeType |
String |
The MIME type string value to transform to a |
Example
This example transforms a MIME type string value without parameters
to a MimeType
value.
Source
1
2
3
4
5
%dw 2.0
import * from dw::module::Mime
output application/json
---
fromString("application/json")
Output
1
2
3
4
5
6
7
8
{
"success": true,
"result": {
"type": "application",
"subtype": "json",
"parameters": {}
}
}
Example
This example transforms a MIME type string value that includes a parameters
to a MimeType
value.
Source
1
2
3
4
5
%dw 2.0
import * from dw::module::Mime
output application/json
---
fromString("multipart/form-data; boundary=the-boundary")
Output
1
2
3
4
5
6
7
8
9
10
{
"success": true,
"result": {
"type": "multipart",
"subtype": "form-data",
"parameters": {
"boundary": "the-boundary"
}
}
}
Example
This example transforms an invalid MIME type string value.
Source
1
2
3
4
5
%dw 2.0
import * from dw::module::Mime
output application/json
---
fromString("Invalid MIME type")
Output
1
2
3
4
5
6
{
"success": false,
"error": {
"message": "Unable to find a sub type in `Invalid MIME type`."
}
}
toString
toString(mimeType: MimeType): String
Transforms a MimeType
value to a string representation.
Parameters
Name | Type | Description |
---|---|---|
mimeType |
|
The MIME type value to transform to a |
Example
This example transforms a MimeType
value without parameters
to a string representation.
Source
1
2
3
4
5
%dw 2.0
import * from dw::module::Mime
output application/json
---
toString({'type': "application", subtype: "json", parameters: {}})
Output
1
"application/json"
Example
This example transforms a MimeType
value that includes a parameters
to a string representation.
Source
1
2
3
4
5
%dw 2.0
import * from dw::module::Mime
output application/json
---
toString({'type': "multipart", subtype: "form-data", parameters: {boundary: "my-boundary"}})
Output
1
"multipart/form-data;boundary=my-boundary"
Types
MimeType
DataWeave type for representing a MIME type. Supports the following fields:
-
type
: Represents the general category into which the data type falls, such as 'video' or 'text'. -
subtype
: Identifies the exact kind of data of the specified type the MIME type represents. -
parameters
: Parameters attached to the MIME type.
1
{ "type": String, subtype: String, parameters: MimeTypeParameter }
MimeTypeError
DataWeave type of the data that returns when a fromString
function fails.
Supports the following fields:
-
message
: The error message.
1
{ message: String }
MimeTypeParameter
DataWeave type for representing a MIME type parameter.
1
{ _?: String }