The Modify Response Rule allows you to mock the response body of an HTTP request.
Popular use cases
- You want to work on front-end while back-end is not available or ready yet.
- You want to test application behaviour when provided altered data.
-
You want to simulate errors by returning different status codes.
-
You want to modify API responses but don't have access to the back-end.
Options
-
Select Resource Type
-
REST API: To modify XHR/Fetch requests from web pages.
-
GraphQL: To Target & Modify GraphQL APIs using operations.
-
HTML/JS/CSS(Desktop app only) - To work with HTML/JS/CSS files.
- Source Condition: If the request URL matches this source condition, then only the rule is applied. You can use
URL
,Host
, orPath
withRegex
,Contains
,Wildcard
orEquals
to match the source request. Learn more about source conditions here.
- Source Filters: This helps you define better targeting conditions and restrict rules to be applied on specific webpages (or domains), request types, request methods, or request payloads. Learn more about source filters here.
- GraphQL Operation(only in the case of GraphQL) - Enables matching the operation you want to target in case of GraphQL APIs.
- Response Status Code: Update the Response status code. (2XX, 3XX, 4XX, 5XX..). If left empty, the original status code is returned.
-
Response Body
-
Static Response Modification - In this mode, you can enter the response body that you want as the response.
-
Programmatic Modification Script (JS) - Write a JavaScript script that can modify the existing response programmatically. The following arguments are available to create a response programmatically:
-
method (string)- HTTP Method of the request like -
GET
,POST
,PUT
,DELETE
,PATCH
-
URL (string)- Complete the URL of the request including query parameters.
-
response (string) - Original response body as a string.
-
responseType (string) - The content type of the original response.
-
requestHeaders (Object) - The request headers are sent to the server.
-
requestData (string) - The HTTP request payload in case of
POST
,PUT
&PATCH
requests. -
responseJSON(JSON object)- The original response body as a JSON Object.
-
Response Returned (JSON or String) - A new response can be generated in string or JSON format, Requestly can handle both responses.
-
-
Local File(Desktop App Only)
In case you have your response in a file, browse the file and its content will be served as response.-
Advanced Usage: Wildcard substitution
For regex and wildcard source URL conditions you can serve different local files based on the request URL pattern using group substitution in the file path using $1, $2, etc. This allows you to use parts of the matched URL to construct the file path dynamically: $1 refers to the first captured group, $2 refers to the second captured group and so on.
Example-
Regex source URL condition: /(.+)\.example\.com\/scripts\/(.+)/ig/
-
File Path:
/Desktop/Documents/app/scripts/env/$1/$2
-
When a request is made to a URL like
https://beta.example.com/scripts/getUsers.js
, Requestly will:-
beta
is extracted using the first captured group ($1). -
getUsers
is extracted using the second captured group ($2). -
Requestly will then look for a file named
getUsers.js
in the/Desktop/Documents/app/scripts/env/beta/
directory. -
If the file exists, the content of that file will be served as response.
-
-
-
-
- Test this rule - This box will appear only after you save the rule, it accepts a URL and opens it in a new tab for you to test. It comes with an option to capture screen, console & network details, if enabled Requestly will capture these details in SessionBook for debugging later.
Targeting GraphQL requests
You can target GraphQL requests using the operation name in request body. To do so add:
- Payload JSON Key, e.g.,
operationName
-
Value, e.g.,
getUsers
To learn more about this checkout GraphQL Support.
Shared state
You can store and retrieve data within and across Modify Request and Modify Response rules using shared state in programmatic javascript mode.
Guides
- Modifying Response asynchronously: Async modifications lets you perform some long running tasks before returning the response
- Modifying Non Existent Domain: Helpful for frontend developers when the backend isn't ready yet.