Hi I am trying to modify the requests by replacing the request body argument with a particular value. I need to modify the request body for multiple requests. Below is the code I have written
static function OnBeforeRequest(oSession: Session) {
if(oSession.HostnameIs("abc.com") && oSession.oRequest.headers.HTTPMethod == "POST") {
// Convert the request body into a string
var oBody = System.Text.Encoding.UTF8.GetString(oSession.requestBodyBytes);
// Convert the text into a JSON object
var j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
//Change the ConsistId value
if(j.JSONObject["cId"] == 0) {
// MessageBox.Show(j.JSONObject["cId"]);
j.JSONObject["cId"] = 12345678;
// Convert back to a byte array
var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
// Convert json to bytes, storing the bytes in request body
var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
oSession.RequestBody = mod;
} else if(j.JSONObject["search"]["con"]["cId"] == 0) {
j.JSONObject["search"]["con"]["cId"]= 12345678;
// Convert back to a byte array
var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
// Convert json to bytes, storing the bytes in request body
var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
oSession.RequestBody = mod;
}
}
}
If I try to modify only first request, no error is thrown, however, for second request modification where body is like this
{"search":{ "con":{"cId":12345678}}}
gives me below error:

Could someone let me know what is wrong here.
Debugging Fiddler Scripts can be quite challenging as there is no line number displayed which causes the problem.
In you script I assume the problem are the comparisons
if(j.JSONObject["cId"] == 0)
if(j.JSONObject["search"]["con"]["cId"] == 0)
The main problem is that j.JSONObject["cId"] does not return an integer as you expect. Instead the [String] accessor returns an JsonObject. When assigning a new value as in your first if section .Net somehow fixes the assignment somehow. But if you retrieve and compare the value this does not work automatically.
In my code I solved a similar problem by this workaround by changing the comparison to json.JSONObject["someitem"].toString().Equals("0").
Note that the code line j.JSONObject["search"]["con"]["cId"] has another problem depending on the responses you receive. If you get a JSON structure that is different to what you expect (e.g. no search, con or cId item) then this line will fail.
You should better check if each element really exist:
var cid = j.JSONObject["cId"];
if (cid != null && cid.toString().Equals("0") {
...
} else {
var search = j.JSONObject["search"];
if (search != null) {
var con = search["con"];
if (con != null) {
if (con["cId"].toString().Equals("0")) {
...
}
}
}
}
BTW: var oBody = System.Text.Encoding.UTF8.GetString(oSession.requestBodyBytes); can be simplified to oSession.GetResponseBodyAsString(); assuming the content encoding is correctly set on the response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With