Hello,
I would like to replace a value of a parameter in request body when another parameter equals to a value. I tried some examples but unable to achieve.
As an example,
While page is loading, it’s requesting with some parameters;
Action=Go
Key=1
Count=5
etc.
I would like to replace another value of parameter(let’s say Count in above example) but only when Action=Go, otherthan that value i don’t want to change and leave as alone without touching.
Hey @Oykun_Satis, You can use Modify API Request Body rule type to change the request body before it is sent to the server.
There are two ways to modify the request body
Override the complete body (Static body option)
Change the existing request body using Script (Programmatic option)
You should choose the programmatic option and define the script like this
function modifyRequestBody(args) {
const { method, url, body } = args;
// Change request body below depending upon request attributes received in args
if (body.Action === "Go") {
body.Count = 1 // Change the value of Count
}
return body
}
There was a bug in this feature that we have recently fixed and pushed to Chrome Web Store. Make sure you are on the latest version v23.1.4
Hi Sachin,
First of all thanks a lot for your reply.
I updated my version and after i tried with the code above seems working, at least the page is not broken like yesterday and when i check both chrome dev and requestly logs seems intercepting however it doesn’t actually. Seems like it intercept correctly without actually applying the rule. I tried also static way if somehow it’s applying and seems Static part working. So seems something wrong with JavaScript option.
Hi @Oykun_Satis. Can you please share the rule with us to investigate further?
In rule page, click “Share” button, select specific people, enter “vaibhav@requestly.io" and copy the link.
Also, if possible please share the website URL where you are testing it, so we can have a closer look.
Please note that with any change in “Modify Request body” rule, you have to reload your page.
However for website i don’t think it will make sense since website i’m working have some authentications ,referer requirements and token exchanges which you cannot simulate.
Thanks @Oykun_Satis for sharing the rule. The rule seems fine. The rule source filter you added on page URL (using funnel button) was not needed though.
I hope the original request body is a JSON. You may confirm the same in browser inspect > Network > Payload tab.
@Oykun_Satis Looks like this is a form submission case and the request payload is a Form data and not a JSON.
Unfortunately, Requestly extension does not yet support the form submission use-case and can modify request body or response only when the API is triggered using JavaScript XHR or fetch methods.
I can help only if the API request is using XHR or fetch. You may confirm it from the value of Type column for the request in Network panel.
Hi Vaibhav,
Actually it’s a XHR as you can see screenbelow.
Also if i use Static type in the Requestly rather than using JavaScript it’s working but as i mentioned above it’s not covering my use case(conditional post request).
I’ll try it on desktop app as well and let you know.
Hi Again
I tried in the desktop app but even i can see the traffic passing through desktop app it’s not applying rule and nothing on Rules Applied column.
Hi @Oykun_Satis,
Since it’s an XHR, it might work. Let me share the updated code snippet in some time.
Otherwise, I will guide you on how to set up on Desktop app.
Meanwhile, can you share the value (or format) you entered in static mode? If it was a JSON, it means that your API supports both Form data and JSON request body.