Skip to main content

Conditional API Responses in mockforme

With mockforme, you can create conditional responses for your APIs, allowing you to simulate different behaviors based on request parameters, such as query parameters or request bodies. This feature is useful for testing how your system reacts under varying conditions.

How to Enable Conditional Responses

By default, the Conditional Response toggle is turned off. When it's off, you'll see a Status column where you can enable or disable individual responses.

Conditional Response switch off – mockforme

Problem: Handling Conditional Responses

Let’s say you need to return different responses depending on the query parameter passed to the API.

Example:

  • API Endpoint: /search/query

  • Method: GET

  • If the query parameter is ?q=phone, return a Phone response.

  • If it's ?q=laptop, return a Laptop response.

To implement this behavior, turn on the Conditional Response toggle and define your conditions.

Enable Conditional Response

Once the Conditional Response toggle is on, the Status column is replaced by a Condition button under the Action column, as shown in the screenshot below.

Conditional Response switch on – mockforme

Click the Condition button to add a conditional logic rule. Here, you can define the condition based on query parameters, request body, or other request attributes.

Add a Condition

In the Add Condition popup, you can specify the condition that determines when a particular response should be triggered. For instance, you might check if the request contains a query parameter q with a specific value (e.g., phone):

Add condition for mock API response – mockforme

Supported System Variables for Conditional Responses

mockforme provides the following system variables to reference within your conditions:

/*
* Access query parameters using mfm.query
* Example: If the query parameter "q" equals "phone", return true
*/
mfm.query

/*
* Access request body (for POST or PUT methods) using mfm.body
* Example: If mfm.body.email equals "test@mockforme.com", return true
*/
mfm.body