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.
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.
Additionally, Requestly supports dynamic response modification, enabling the use of JavaScript to conditionally alter responses based on the original request or server response.
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
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.
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.
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
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.
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.
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.
-
Programmatic Modification Script (JavaScript): Use JavaScript to dynamically modify responses. 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 tick the “Server 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.
-
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.
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.
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.