Supported Media Types in Mule

 What is a Media Type?

     Media Type is also known as MIME type, which stands for Multipurpose Internet Mail Extensions. It is a two-part identifier for file formats and contents transmitted over the Internet. Since APIs are network applications that are using the HTTP transport, media types are the identifiers that describe the request and response format that the API consumes and produces. You can indicate the media type in these HTTP headers: Content-Type and Accept. A common example of a media type is text/html; charset=UTF-8, wherein text is the type and html is the subtype, which may also have optional parameters such as charset that indicate the character encoding, e.g. Unicode (8-bit, 16-bit), ASCII, etc.

    Mule supports a lot of media types, not just XML or JSON. As per the MuleSoft documentation, below are the supported media types:


    The great thing with Mule is that it is data agnostic, as long as you use one of the media/ MIME types in that list. Once the request data is received, you can process and manipulate the data using the DataWeave expression language, which means that the scripts to read/write XML, JSON, or CSV data will be the same, except for some minor differences in the structure (such as always having a root element for XML). You don't have to write different scripts for XML manipulation, such as XPath or even Java code.
Where do we use these media types in Mule? Here are some of the places where I have used media or MIME types in Mule:
1. In the RAML when defining the request and response format. For example, this endpoint returns a JSON format in the body.

2. In the output directive of the DataWeave script, which obviously is the format of the output of the script! :)
3. In reading the payload in string or binary format and parsing the content to the specified format, using the read function. For example, you have this payload which is a String type, and you expect it to be in a JSON format. You can use the read function to parse the string into a JSON object.
4. In writing a value as a string or binary in the specified format, using the write function. This is just the reverse, which is from Object to String/Binary. For instance, you have a Java Object that you want to render as a String format.

If you have any questions or suggestions, please feel free comment in this post. :)

Comments

Post a Comment

Popular posts from this blog

XML Schema and JSON Schema Validation in Mule 4

Parsing a JSON String and Converting it to XML in TIBCO

Using XML To Java in TIBCO BW