-
Notifications
You must be signed in to change notification settings - Fork 4
Some example script
NGUYEN DUY QUOC KHANH edited this page Feb 28, 2024
·
4 revisions
Version < 2.02
To modify url parameter:
- You must get url parameter from path by using path() method
- After get url parameter, modify it, build new path and pass it to withPath() method Script:
path = jsrequest.path();
path = path.substring(2, path.length);
paramPairs = path.split('&');
params = []
for (var i = 0; i < paramPairs.length; i++) {
var param = paramPairs[i].split('=');
params.push({"name" : param[0], "value": param[1]});
}
params[0].value = "modified";
new_path = "/?" + params[0].name + "=" + params[0].value;
jsresult.request= jsrequest.withPath(new_path);
jsresult;Result:
Original request:

Modified request:

- Like script above Script:
body = jsrequest.bodyToString();
paramPairs = body.split('&');
params = []
for (var i = 0; i < paramPairs.length; i++) {
var param = paramPairs[i].split('=');
params.push({"name" : param[0], "value": param[1]});
}
params[0].value = "modified";
new_body = params[0].name + "=" + params[1].value;
jsresult.request= jsrequest.withBody(new_body);
jsresult;Result:
Original request:

Modified request:

Script:
body = jsrequest.bodyToString();
json_body = JSON.parse(body);
json_body.a = 2;
new_raw_body = JSON.stringify(json_body);
jsresult.request= jsrequest.withBody(new_raw_body);
jsresult;Result:
Original request:

Modified response:

Script:
modified_request = jsrequest.withPath("/hash");
modified_request = modified_request.withHeader("Hash", "123");
modified_request = modified_request.withBody("c=2");
jsresult.request= modified_request;
jsresult;Result:
Original request:

Modified request:

@Copyright ngduyquockhanh