Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Modify JSON responses using Fiddler Scripts

Tags:

proxy

fiddler

Below is the response(body) that I get from my server. I'm looking to modify some of the Key Value Pairs. I used the the following approach:

var bodystr=oSession.GetResponseBodyAsString();
var bodyjson=Fiddler.WebFormats.JSON.JsonDecode(bodystr); 

But the bodyjson does not have any content that I expect. (I tried to use the MessageBox.Show(bodyjson.Sales.Qty); but this returns me an error.)

{
    "Sales" : {
        "Qty" : 1,
        "Item" : {
            "value" : "7"
        },
        "TaxCode" : {
            "value" : "NON"
        },
        "UnitPrice" : 3
    },
    "LineNum" : 0,
    "DetailType" : "Sales",
    "Amount" : 3,
    "Id" : "1"
}

Is there any way, apart from string replace methods, to make changes to the JSON responses?

like image 668
Code hunter Avatar asked Jul 05 '26 21:07

Code hunter


2 Answers

The post above helped me get this working, my full example is as follows

The JSON that I wanted to change before it was sent as follows

{
   "ConsistId":"09C31636-0D8E-4C92-B09C-8413366E2D79",
   "UserName":"Test",
   "Date":"2016-07-19T13:10:00"
}


 static function OnBeforeRequest(oSession: Session) {

    if(oSession.HostnameIs("localhost")) {
        oSession["ui-backcolor"] = "lime";
        oSession["ui-bold"] = "Bold text here"

        // 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
        j.JSONObject["ConsistId"] = "A9C01636-0D8E-4C92-B09C-8413366E2D79";

        // 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;

    }
...

This changed the request, so now need to figure out how to display a prompt to populate the guid id

like image 92
Andrew Avatar answered Jul 08 '26 22:07

Andrew


Fiddler's JsonDecode function creates an object; it doesn't create the type of object you expect and even if it did, changing values in that object wouldn't have any automatic impact on the string making up the response body.

See http://www.telerik.com/forums/how-to-use-fiddler-webformats-json-jsondecode for some insight into how this object works.

You'd need to do something like bodyjson.JSONObject["Sales"]["Qty"] to get the value. After you make any changes, you'd need to call JsonEncode on the object to get a string and then set the response's body to that string.

    var s = '{"Sales" : {  "Qty" : 8,     "Item" : {            "value" : "7"          },          "TaxCode" : {            "value" : "NON"          },          "UnitPrice" : 3        },        "LineNum" : 0,        "DetailType" : "Sales",        "Amount" : 3,        "Id" : "1"}';
    var j = Fiddler.WebFormats.JSON.JsonDecode(s);
    MessageBox.Show(j.JSONObject["Sales"]["Qty"]);
    j.JSONObject["Sales"]["Qty"] = 4;
    MessageBox.Show(Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject));

If you simply want to make a trivial change to the body text, don't bother turning the string into an object at all, simply change the string itself directly.

like image 26
EricLaw Avatar answered Jul 08 '26 21:07

EricLaw