Skip to content

Some example script

NGUYEN DUY QUOC KHANH edited this page Feb 28, 2024 · 4 revisions

Version < 2.02

Mofidy the url parameter in request

To modify url parameter:

  1. You must get url parameter from path by using path() method
  2. 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:

image

Modified request:

image

Modify the body parameter in 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:

image

Modified request:

image

Modify the json body in 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:

image

Modified response:

image

Modified path, header and body in 1 request

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:

image

Modified request:

image

Clone this wiki locally