Skip to content

Commit

Permalink
Create new requests docs entries
Browse files Browse the repository at this point in the history
  • Loading branch information
255kb committed Oct 26, 2024
1 parent 380a1cc commit b868579
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 236 deletions.
2 changes: 1 addition & 1 deletion content/blog/celebrating-4000-github-stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Last year, we added many features for the desktop application and the CLI. It's
- Mockoon now supports [custom TLS certificates](/docs/latest/server-configuration/serving-over-tls/#provide-your-own-certificate) to serve your mock securely without relying on Mockoon's self-signed certificate.
- We improved the system of [rules](/docs/latest/route-responses/dynamic-rules/) by adding a lot of features: support for cookies, reordering, rule inversion (coming soon!), rule disabling (coming soon!), `multipart/form-data` support (coming soon!)
- We removed Google Analytics from the desktop application to increase your privacy.
- We added [XML support](/docs/latest/response-configuration/xml-support/) for both templating helpers and rules.
- We added [XML support](/docs/latest/requests/supported-body-formats/#xml-support) for both templating helpers and rules.
- We added more binaries formats: Apple Silicon, Windows portable, and Microsoft Store.
- The [CLI](https://github.com/mockoon/mockoon/tree/main/packages/cli#mockoon-cli-start) can now run multiple mocks at once, log the complete HTTP transaction (`--log-transaction` flag), and run as a foreground process (`--daemon-off` flag).

Expand Down
2 changes: 1 addition & 1 deletion content/blog/new-storage-system-git-data-sharing.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ Starting with this version, you can load them directly in Mockoon by clicking on

## XML support

Last but not least, we added [support for the XML format](docs:response-configuration/xml-support). It means that you can now reuse part of the entering XML bodies with various [templating helpers](docs:templating/overview) (`body`, etc.) and [define rules](docs:route-responses/dynamic-rules) to serve different responses depending on the XML content.
Last but not least, we added [support for the XML format](docs:requests/supported-body-formats#xml-support). It means that you can now reuse part of the entering XML bodies with various [templating helpers](docs:templating/overview) (`body`, etc.) and [define rules](docs:route-responses/dynamic-rules) to serve different responses depending on the XML content.
10 changes: 2 additions & 8 deletions content/docs/latest/api-endpoints/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,9 @@ You can also retrieve the route parameters by using the `{{urlParam 'paramName'}

Routes **must** be declared without query parameters as they are not part of the route path. They can only be added to the request when calling an endpoint.

Query parameters can be retrieved by using the `{{queryParam 'paramName'}}` [templating helper](docs:templating/mockoon-request-helpers#queryparam).
Query parameters can be retrieved with the **[`queryParam`](docs:/docs/latest/templating/mockoon-request-helpers/#queryparam) and [`queryParamRaw`](docs:/docs/latest/templating/mockoon-request-helpers/#queryparamraw) helpers** and are available in the **[response rules](docs:route-responses/dynamic-rules)** to check their content and serve different responses based on them.

#### Query parameters arrays and objects

Mockoon is using [qs](https://www.npmjs.com/package/qs) to parse the query string in a object usable in our templating or rules systems. It supports both arrays and objects.
To pass arrays and objects in the query string of a request, you must use the following syntax:

- for objects: `?param1=test&obj[prop1]=value`.
- for arrays: `?param1=test&array[]=value1&array[]=value2` or `?param1=test&array[0]=value1&array[1]=value2` or `?param1=test&array=value1,value2`.
**Arrays and objects are supported** in the query string. For more information, please refer to the [query parameters documentation](docs:requests/query-parameters).

### Temporarily disable a route

Expand Down
57 changes: 57 additions & 0 deletions content/docs/latest/requests/query-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Query parameters
meta:
title: Query parameters usage and parsing
description: Learn how Mockoon parses query parameters and how to use them in your mock API responses.
order: 155
---

# Query parameters

---

## Query parameters parsing

Routes must be **declared without query parameters** as they are not part of the route path. They can only be added to the request when calling an endpoint.
Mockoon is using the [qs NPM package](https://www.npmjs.com/package/qs) to parse query parameters in the URL. `qs` supports **nested objects and arrays** in the query string.

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.

Example URL with query parameters:

```text
http://localhost:3000/route?param1=test&param2=test2&array[]=value1&array[]=value2&obj[prop1]=value
// alternative array syntaxes
array[0]=value1&array[1]=value2
array=value1,value2
```

Parsed query parameters:

```json
{
"param1": "test",
"param2": "test2",
"array": ["value1", "value2"],
"obj": {
"prop1": "value"
}
}
```

## Where can I access the query parameters?

The parsed query parameters will be available **everywhere [templating helpers](docs:templating/overview) are supported**: response body, headers, rules, data buckets, callbacks, etc. You will be able to use the [`queryParam`](docs:/docs/latest/templating/mockoon-request-helpers/#queryparam) and [`queryParamRaw`](docs:/docs/latest/templating/mockoon-request-helpers/#queryparamraw) to access the parsed parameters and create dynamic responses based on it.

The parameters will also be **available in the [response rules](docs:route-responses/dynamic-rules)** to check their content and serve different responses based on them.

Example `queryParam` templating helper usage:

```handlebars
{{queryParam 'param1'}}
<!-- object-path syntax -->
{{queryParam 'obj.prop1'}}
<!-- JSONPath syntax -->
{{queryParam '$.obj.prop1'}}
```
164 changes: 164 additions & 0 deletions content/docs/latest/requests/supported-body-formats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
---
title: Supported body formats
meta:
title: Supported request body formats
description: Learn about the supported request body formats in Mockoon, including JSON, form data, and URL-encoded data. How to use them and related options.
order: 150
---

# 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:

- [`application/json`](#json-support)
- [`application/x-www-form-urlencoded`](#x-www-form-urlencoded-support)
- [`multipart/form-data`](#multipart-form-data-support)
- [`application/xml`](#xml-support)

Invalid bodies will be parsed as strings.

## What can I do with the parsed body?

The parsed body will be available **everywhere [templating helpers](docs:templating/overview) are supported**: response body, headers, rules, data buckets, callbacks, etc. You will be able to use helpers like [`body`](docs:/docs/latest/templating/mockoon-request-helpers/#body) and [`bodyRaw`](docs:/docs/latest/templating/mockoon-request-helpers/#bodyraw) to access the parsed body content and create dynamic responses based on it.

The body will also be **available in the [response rules](docs:route-responses/dynamic-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:

```json
{
"key": "value",
"nested": {
"key": "value"
}
}
```

Example `body` templating helper usage:

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

> 📘 Please refer to the [templating](docs:templating/overview) documentation for more information on how to use the `body` helper.
> 🛠️ Use our [JSON validator tool](/tools/json-validator/) to validate your JSON body before using it in Mockoon.
## 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](https://www.npmjs.com/package/qs). qs supports nested objects and arrays in the URL-encoded body.

Example URL-encoded body:

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

> 💡 The same syntax applies to the [parsing of query strings](docs:api-endpoints/routing#query-parameters-arrays-and-objects).
Parsed body:

```json
{
"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](https://www.npmjs.com/package/busboy). 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:

```plaintext
--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:

```json
{
"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](https://www.npmjs.com/package/xml-js) 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:

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

JSON equivalent (compacted):

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

> 📘 Please refer to [xml-js documentation](https://www.npmjs.com/package/xml-js) for more detail on how the XML is parsed.
> 🛠️ Use our [XML to JSON converter](/tools/xml-to-json/) to get a preview of how Mockoon will convert your XML to JSON. You can also use our [XML validator](/tools/xml-validator/) to make sure your XML is valid.
51 changes: 0 additions & 51 deletions content/docs/latest/response-configuration/xml-support.md

This file was deleted.

4 changes: 3 additions & 1 deletion content/docs/latest/route-responses/dynamic-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ This field supports [templating helpers](docs:templating/overview) to dynamicall
- keep empty to match against the full raw body content.
- use a path to access one of its properties. Two syntaxes are supported, [object-path](https://www.npmjs.com/package/object-path) or [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus). When using object-path, properties containing dots are supported by escaping the dots: `key.key\.with\.dot`.
Fetching object properties is compatible with request's bodies of `Content-Type` `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `application/xml`, `application/soap+xml` or `text/xml`.
_Please note that XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. Refer to this [page](docs:response-configuration/xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties._
_Please note that XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. Refer to this [page](docs:requests/supported-body-formats#xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties._
_Please also note that `multipart/form-data` only supports fields. Uploaded files will be ignored._
> 📘 Check the [supported requests body formats](docs:requests/supported-body-formats) documentation for more information on how the request body is parsed.
- **query parameter**: either provide a property name like `filter` or a path if the query parameter is an object `filter.primary`.
> 📘 Check the [query parameters](docs:requests/query-parameters) documentation for more information on how they are parsed.
- **headers**: a header name like `Accept` or `Content-Type`.
- **cookies**: the cookie name like `Session-id`.
- **route parameter**: a route parameter name without the colon (":"), `:userId` becoming `userId`.
Expand Down
8 changes: 6 additions & 2 deletions content/docs/latest/templating/mockoon-request-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Mockoon offers the following helpers which can return information relative to th
Get the value at a given `path` from the request's body if the `Content-Type` is set to `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `application/xml`, `application/soap+xml` or `text/xml`. This helper is designed to retrieve data, **stringify** it, it order to use it directly in a response. To reuse the retrieved data (strings, booleans, arrays, etc.) with other helpers (`each`, `if`, etc.), use the [`bodyRaw` helper](#bodyraw) below.

- The `path` supports two syntaxes, [object-path](https://www.npmjs.com/package/object-path) or [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus). When using object-path, properties containing dots are supported by escaping the dots: `key.key\.with\.dot`.
- XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. Please refer to this [page](docs:response-configuration/xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
- XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. Please refer to this [page](docs:requests/supported-body-formats#xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
- `multipart/form-data` supports fields and files. Uploaded files will not be stored, but their metadata (`filename`, `mimetype`, `size`) will be available in the request body:

```json
Expand All @@ -48,6 +48,8 @@ Get the value at a given `path` from the request's body if the `Content-Type` is
- If no value is present at the requested `path`, the default value will be used.
- A third parameter (boolean) can be set to true to returns a stringified value even if it's a primitive.

> 📘 Check the [supported requests body formats](docs:requests/supported-body-formats) documentation for more information on how the request body is parsed.
> 🛠️ Use our online [JSONPath and object-path evaluator](/tools/json-object-path-evaluator/) to test your JSONPath or object-path syntaxes and view the results in real-time.
| Arguments (ordered) | Type | Description |
Expand Down Expand Up @@ -77,7 +79,7 @@ Get the value at a given `path` from the request's body if the `Content-Type` is
Get the **raw** value (string, boolean, array, etc.) at a given `path` from the request's body if the `Content-Type` is set to `application/json`, `application/x-www-form-urlencoded`, `multipart/form-data`, `application/xml`, `application/soap+xml` or `text/xml`. This "raw" helper is designed to give you access to the values to use them with other helpers (`each`, `if`, etc.). To directly use the stringified data in the response, use the [`body` helper](#body) above.

- The `path` supports two syntaxes, [object-path](https://www.npmjs.com/package/object-path) or [JSONPath Plus](https://www.npmjs.com/package/jsonpath-plus). When using object-path, properties containing dots are supported by escaping the dots: `key.key\.with\.dot`.
- XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. PLease refer to this [page](docs:response-configuration/xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
- XML bodies are parsed using [xml-js](https://www.npmjs.com/package/xml-js) package. PLease refer to this [page](docs:requests/supported-body-formats#xml-support) or the package documentation for more information on how the XML is parsed and how to fetch specific properties.
- `multipart/form-data` only supports fields and files. Uploaded files will not be stored, but their metadata (`filename`, `mimetype`, `size`) will be available in the request body:

```json
Expand All @@ -96,6 +98,8 @@ Get the **raw** value (string, boolean, array, etc.) at a given `path` from the
- If no value is present at the requested `path`, the default value will be used.
- This helper allows the use of `body` within handlebars' helpers such as `{{#each}}` and `{{#if}}`.

> 📘 Check the [supported requests body formats](docs:requests/supported-body-formats) documentation for more information on how the request body is parsed.
> 🛠️ Use our online [JSONPath and object-path evaluator](/tools/json-object-path-evaluator/) to test your JSONPath or object-path syntaxes and view the results in real-time.
| Arguments (ordered) | Type | Description |
Expand Down
Loading

0 comments on commit b868579

Please sign in to comment.