Unable to modify response for non-existent API

In my web application, I want to specify response for an API which is yet to be developed.

I created a Modify API Response rule in Requestly as:

If request URL Equals https://services.example.com/api/data
Response Option: Static
Response Body:
{
  "items": [
    { "id": "a", "name": "A" },
    { "id": "b", "name": "B" },
    { "id": "c", "name": "C" }
  ]
}

But I notice that the rule does not apply and I see a console error in browser:
Uncaught (in promise) TypeError: Failed to fetch

Requestly seems to modify response only for APIs which return valid response and not for non-existent APIs.

Workaround is to create a mock API under Mocks section and specify the response. We get a public endpoint which we can use in our web application. But once the original API is ready, we would again need code changes to switch to original endpoint. To avoid this, we would need to create another rule which redirects original endpoint to mock endpoint.

1 Like

This feature has been rolled out in Requestly.
Users may now create “Modify API Response” rule for non-existent APIs as well and consume the response in their UI application without even waiting for backend APIs to be ready.

This is how rule definition looks like:


Please note that now the user may also specify response status code along with response body.

Here is a screenshot of how I could make the API request via browser console and get the desired output:

1 Like

Just to add, user may also define a dynamic response based on request parameters.
For instance, the response should return { productId: "123" } if request body sent { id: "123" }.

Choose programmatic modification of response.

function modifyResponse(args) {
  const {requestData} = args;
  
  return JSON.stringify({
    "productId": requestData.id,
    "items": [
      { "id": "a", "name": "A" },
      { "id": "b", "name": "B" },
      { "id": "c", "name": "C" }
    ]
  });
}

Here is the screenshot of usage:

thanks for the awesome information.

2 Likes