Modify API Response

Learn how to modify API responses in Requestly with Dynamic Modification. Simulate edge cases, test with custom data, and create API mocks for faster front-end development and QA workflows


The Modify API Response Rule in Requestly allows developers to modify API responses directly without requiring back-end access. It is often used to simulate edge cases, replicate bugs, and support QA workflows. With Dynamic Modification, developers can use JavaScript to conditionally alter responses based on the original request or server response

Why Use Modify API Response Rule?

Modify API Response Rules are useful in several scenarios:

  • Frontend Development: Work seamlessly even if the backend is unavailable or incomplete.

  • Custom Testing: Test application behavior with modified or custom data.

  • Error Simulation: Trigger specific errors by modifying response status codes.

  • Backend Independence: Make changes to API responses without requiring backend changes.

This rule can also be used to create API Mocks that are beneficial during development and QA cycles.

Steps to Configure a Modify API Response Rule

1

Create a Response Rule

Navigate to the HTTP Rules section in the Requestly app. Select Add Rule and choose the Modify API Response option. Give it a meaningful name and description.

2

Select Resource Type

Choose the resource type for modification:

  • REST API: Modify XHR/Fetch requests initiated by web pages.

  • GraphQL: Target and modify GraphQL APIs by operation.

  • HTML/JS/CSS (Desktop App Only): Modify static files for desktop applications.

3

Define Matching Criteria

Specify the criteria for matching API responses. Define the URL pattern or use response attributes such as status code to identify the target response. Use Source Conditions like:

  • URL, Host, or Path with Regex, Contains, Wildcard, or Equals

  • Filters for specific webpages, request types, methods, or payloads

4

Filter GraphQL Queries (Optional)

If targeting GraphQL APIs, filter queries by their operation name. Add the Payload JSON Key (e.g., operationName) and specify the corresponding Value (e.g., getUsers) to target specific GraphQL operations.

5

Change Response Status Code

Modify the status code of the response using the dropdown menu. Choose from:

  • 2XX for successful responses

  • 4XX for client errors

  • 5XX for server errors

If left empty, the original status code remains unchanged.

6

Modify Response Data

Choose one of the following methods to modify the response data:

  • Static Response Modification: Input a static value as the response body.

  • Dynamic Modification Script (JavaScript): Use JavaScript to modify responses dynamically. The script provides access to parameters like:

    • method: HTTP Method (GET, POST, etc.)

    • url: Full URL of the request

    • response: Original response body (string)

    • responseType: Content type of the original response

    • requestHeaders: Headers sent with the request

    • responseJSON: Original response parsed as a JSON object

    Example:

    function modifyResponse({ responseJSON }) {
        responseJSON.modifiedKey = "modifiedValue";
        return responseJSON;
    }
    

    You can check the “Serve this response body without making call to the server” checkbox as the label suggests it will ensure you get the response locally without hitting your API server.

7

Save and Activate the Rule

Click Save to store the rule and toggle it ON. This will apply the rule to the defined source conditions.

8

Test the Rule

This feature is available on the browser extension only.

To ensure the rule is working correctly, click the Test button and enter the URL of the page where you want to apply this rule. This will open the webpage in a new tab, with a widget showing the status of applied rules and logs any debugging information in the SessionBook.

Changes applied by this rule won’t appear in browser devtools due to technical constraints, but are sent with the actual requests.

Desktop App features

Using the Requestly Desktop App gives you more control over network traffic with some additional response modification features like -

Serve local files

Replace API, HTML, JS, or CSS responses with local files. Changes to the file are reflected in real-time—no need to update the rule.

Override HTML/JS/CSS

Intercept and modify static assets like HTML, JavaScript, and CSS. Useful for testing UI changes without touching production code.

Shared State for Advanced Modifications

You can store and retrieve data within and across Modify Request and Modify Response rules using shared state in programmatic JavaScript mode. Learn more about shared state here.

There are multiple ways to validate if a rule is applied or not. Learn more about how to validate rule execution.

Updated on