Supported request body formats


 Request body parsing

Mockoon supports the following request body formats, provided that the request's Content-Type header is correctly set:

Invalid bodies will be parsed as strings.

 What can I do with the parsed body?

The parsed body will be available everywhere templating helpers are supported: response body, headers, rules, data buckets, callbacks, etc. You will be able to use helpers like body and bodyRaw to access the parsed body content and create dynamic responses based on it.

The body will also be available in the response rules to check its content and serve different responses based on it.

As Mockoon is based on JavaScript, the body will be converted into a JSON object internally, which you can manipulate as you would any JSON object using dot notation or array notation.

 JSON support

JSON bodies will be parsed if the request Content-Type is set to application/json with or without extra parameters like charset=utf-8 or vendor-specific subtypes like application/vnd.com.example.object+json.

Example JSON body:

Copy
{ "key": "value", "nested": { "key": "value" } }

Example body templating helper usage:

Copy
{{body 'key'}} <!-- object-path syntax --> {{body 'nested.key'}} <!-- JSONPath syntax --> {{body '$.nested.key'}}

 X-WWW-Form-Urlencoded support

When sending a request containing a valid URL-encoded body and an application/x-www-form-urlencoded Content-Type, Mockoon will parse the URL-encoded body using the qs NPM package. qs supports nested objects and arrays in the URL-encoded body.

Example URL-encoded body:

Copy
key1=value1&key2=value2&array[]=value1&array[]=value2 // alternative array syntaxes array[0]=value1&array[1]=value2 array=value1,value2

Parsed body:

Copy
{ "key1": "value1", "key2": "value2", "array": ["value1", "value2"] }

 Multipart form data support

When sending a request containing a valid multipart form data body and a multipart/form-data Content-Type, Mockoon will parse the form data using the busboy NPM package. Fields will be parsed as strings, and files, while not saved to disk, will be processed and their metadata will be available in the parsed body.

Example multipart form data body:

Copy
--X-BOUNDARY Content-Disposition: form-data; name="key1" value1 --X-BOUNDARY Content-Disposition: form-data; name="key2" value2 --X-BOUNDARY Content-Disposition: form-data; name="file"; filename="file.txt" Content-Type: text/plain file content --X-BOUNDARY--

Parsed body:

Copy
{ "key1": "value1", "key2": "value2", "file": { "filename": "file.txt", "mimetype": "text/plain", "size": 11 } }

 XML support

When sending a request containing a valid XML body and an application/xml, text/xml or application/soap+xml Content-Type, Mockoon will parse the XML using the xml-js NPM package to convert the entering XML. Please note that the xml-js package converts XML into JSON in a particular way, as shown below:

Entering XML body:

Copy
<?xml version="1.0" encoding="utf-8"?> <user userID="123"> <firstname>John</firstname> <lastname>Doe</lastname> </user>

JSON equivalent (compacted):

Copy
{ "_declaration": { "_attributes": { "version": "1.0", "encoding": "utf-8" } }, "user": { "_attributes": { "userID": "123" }, "firstname": { "_text": "John" }, "lastname": { "_text": "Doe" } } }