mockfly logo

Using URL Parameters in the Output

To enhance the realism of our simulated API, it's possible to return the parameters set as dynamic within the URL. Let's say, for the URL /products/:code, we'd like our simulated API output to display a product object with the code from the URL. Here's how to achieve that:

{
    "code": "{{:code}},
    "item": "Apple"
}

In this configuration, our simulated API will substitute the {{:code}} with the code specified as a parameter in the URL. It's crucial to maintain consistency in naming with the URL, or Mockfly won't make any substitutions.

Substituting the body using the request body:

We can modify the response returned by our mock API using the parameters we send in the request body. This is particularly useful, for example, when we want our mock API to return the same ID that we sent in the request body. This is a common use case and can be achieved similarly to how we handle URL parameters. In this context, the syntax would look something like: {{body.foo.bar}}. For instance, if we have the following in the request body:

{
  "account": {
    "id": "123"
  }
}

And in the response body in Mockfly app we have:

{
    "accountId": "{{body.account.id}}"
}

Our mock API would return:

{
    "accountId": "123"
}

This can also be applied to arrays in the same manner. Let's say we send this in the request body:

{
  "account": {
    "id": "123"
  }
}

And in the response body in Mockfly we have:

{
    "accounts": ["{{body.account.id}}"]
}

Our mock API would then return:

{
    "accounts": ["123"]
}

In summary, this substitution can be used anywhere within the response body.

Create a project