Modify Request Body

 

Modify Response Body

Modifying the request payload is the process of changing the data sent to the server when making an HTTP request. You can use Modify Request Body rule to override the API request body with static data or programmatically modify the existing request payload.

Static Request Body Modification

In this mode, you can enter a static request body (JSON) you want to forward to the server.
notion image
Rule working but doesn't show updated request body in devtools
Request Body Modifications will not show up in the browser network devtools due to technical constraints. So your rule might actually be working but only doesn't show the updated Request body in the browser devtools.
  1. Source Condition: Source condition is where you set criteria for the rules. You can use URL, Host, or Path with Regex, Contains, Wildcard, or Equals to match the source request. Learn more about source conditions here.
  1. Static Request Body: Define the static request body which must be passed to the server.
  1. Source Filters: You can define better targeting conditions and restrict rules to apply on specific web pages (or domains), request types, request methods, or request payloads. Learn more about source filters here.

Programmatic Request Body Modification

The existing request body can be modified programmatically using JavaScript.
notion image
Programmatic Modification Script (JS) is where you write a JavaScript script that can modify the existing request body programmatically.

Arguments of modifyRequestBody

  1. method (string)- The HTTP method of the request. GET | POST | PUT | DELETE etc.
  1. url (string) - The request URL.
  1. body (string)- The original request body stringified:
'{"app":"requestly","feature":"modify-request"}'
  1. bodyAsJson (JSON object)- The original request body parsed into JSON object:
{ "app":"requestly", "feature":"modify-request" }

Return type of modifyRequestBody

You can return modified body as string or JSON object.

How to modify the body of a POST request.

We use https://echo.hoppscotch.io/ to test this which echoes back the request.
  1. We make the following POST request.
const options = { method: 'POST', headers: {'content-type': 'application/json'}, body: '{"app":"requestly"}' }; fetch('https://echo.hoppscotch.io/', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err));
  1. Create a Modify Request Body rule uses the following JavaScript. You can find the rule here.
function modifyRequestBody(args) { const { method, url, body, bodyAsJson } = args; // Change request body below depending upon request attributes received in args let newBody; if (method === "POST") { newBody = bodyAsJson; newBody["feature"] = "modify request"; } return newBody; }
  1. You can see the key feature being added to the request body when hoppscotch echoes back the request.

Popular Use Cases:

  • Primarily used in sending additional data in request payload to the API server: There might be situations where additional data needs to be sent in request payloads to the API server when making a POST or PUT request.
  • Modifying GraphQL Queries: GraphQL queries can be modified by modifying the request body of the request. This can be done by changing the query string or variables in the request body.
  • Testing different edge cases: You may modify an API request payload to include an invalid or unsupported field. The server will likely reject the API request and return an error message.

FAQs

Request Rule not working
Rule working but doesn't show updated request body in devtools
Request Body Modifications will not show up in the browser network devtools due to technical constraints. So your rule might actually be working but only doesn't show the updated Request body in the browser devtools.