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

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`."
  }
}

isHandledBy

isHandledBy(base: MimeType, other: MimeType): Boolean

Returns true if the given MimeType value is handled the base MimeType value.

Parameters
Name Type Description

base

MimeType

The MIME type value used as baseline.

other

MimeType

The MIME type value to be validated.

Example

This example tests how MIME types handles several validations.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
%dw 2.0
import * from dw::module::Mime
output application/json

var JSON = {'type': "application", subtype: "json", parameters: {}}
var MULTIPART = {'type': "multipart", subtype: "form-data", parameters: {boundary: "my-boundary"}}
var ALL = {'type': "*", subtype: "*", parameters: {}}
---
{
  a: isHandledBy(JSON, JSON),
  b: isHandledBy({'type': "*", subtype: "json", parameters: {}}, JSON),
  c: isHandledBy({'type': "application", subtype: "*", parameters: {}}, JSON),
  d: isHandledBy(ALL, MULTIPART),
  e: isHandledBy(MULTIPART, ALL),
  f: isHandledBy(JSON, MULTIPART),
  g: isHandledBy(
    {'type': "application", subtype: "*+xml", parameters: {}},
    {'type': "application", subtype: "soap+xml", parameters: {}})
}
Output
1
2
3
4
5
6
7
8
9
{
  "a": true,
  "b": true,
  "c": true,
  "d": true,
  "e": false,
  "f": false,
  "g": true
}

toString

toString(mimeType: MimeType): String

Transforms a MimeType value to a string representation.

Parameters
Name Type Description

mimeType

MimeType

The MIME type value to transform to a String.

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.

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

Definition
1
{ message: String }

MimeTypeParameter

DataWeave type for representing a MIME type parameter.

Definition
1
{ _?: String }