mockfly logo


Dynamic Mock API Responses with MockFly: A Deep Dive with Examples

Dynamic Mock API Responses with MockFly: A Deep Dive with Examples

One of the most valuable features of Mockfly, our API mocking tool, is the ability to create dynamic endpoint responses based on custom rules. In this article, we'll explore this feature in-depth and provide examples to showcase its power and flexibility, focusing on UI for defining rules and examining both request body and query parameters.

Creating Multiple Responses per Endpoint

With MockFly, you can define multiple responses for a single endpoint, allowing you to test different scenarios and outcomes. Using the UI, you can create two responses for a POST request to the "/users" endpoint, each with different data.

Setting Custom Rules

The UI allows you to create custom rules that decide which response to deliver based on conditions involving request body or query parameters. When creating a rule, the UI offers four configurable components:

  1. Source: Choose between "body" or "query params" using a selector.
  2. Property: Enter the property name using a text field.
  3. Comparator: Select either "equal" or "distinct" using a selector.
  4. Value: Input the value to compare using a text field.

For example, you can return the second response if the request body contains a JSON object with a "name" property set to "John" by setting the source to "body", property to "name", comparator to "equal", and value to "John".

Alternatively, you can create a rule based on query parameters. Suppose you want to return the second response only if the query parameter "name" is "John". Set the source to "query params", property to "name", comparator to "equal", and value to "John". This rule will deliver the second response if the condition is met.

Mockfly's dynamic mock API responses feature allows you to define multiple responses per endpoint and control their delivery using custom rules, all through an intuitive graphical user interface. By offering a straightforward way to set rules based on request body properties and query parameters with "equal" or "distinct" comparisons, Mockfly enables you to create more realistic and comprehensive testing scenarios, improving the quality of your final product.