Modify Response Body

The Modify Response Rule allows you to mock the response body of an HTTP request.

  • 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

  1. 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.

  1. Source Condition: If the request URL matches this source condition, then only the rule is applied. You can use URLHost, or Path with Regex, Contains, Wildcard or Equals to match the source request. Learn more about source conditions here.
  1. 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.
  1. GraphQL Operation(only in the case of GraphQL) - Enables matching the operation you want to target in case of GraphQL APIs.
  1. Response Status Code: Update the Response status code. (2XX, 3XX, 4XX, 5XX..). If left empty, the original status code is returned.
  1. 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:

      1. method (string)- HTTP Method of the request like - GET, POST, PUT, DELETE, PATCH

      2. URL (string)- Complete the URL of the request including query parameters.

      3. response (string) - Original response body as a string.

      4. responseType (string) - The content type of the original response.

      5. requestHeaders (Object) - The request headers are sent to the server.

      6. requestData (string) - The HTTP request payload in case of POST, PUT & PATCH requests.

      7. responseJSON(JSON object)- The original response body as a JSON Object.

      8. 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 returned.

  1. 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

 

FAQs

Updated on