JSON values got reveresed while modifying response in fiddler

831 views
Skip to first unread message

sandeep goyal

unread,
Oct 25, 2017, 6:59:47 PM10/25/17
to Fiddler

I am writing a fiddler script to modify the response  below is the code:

static function OnBeforeResponse(oSession: Session) {
        if (m_Hide304s && oSession.responseCode == 304) {
            oSession["ui-hide"] = "true";
        }
        if (oSession.host == "localhost:8000") {

           var sbody = oSession.GetResponseBodyAsString();

           var oJSON1:Fiddler.WebFormats.JSON.JSONParseResult=Fiddler.WebFormats.JSON.JsonDecode(sbody);

           //some junk modification
           var value = oJSON1.JSONObject["n4"];
           value + =1;
            oJSON1.JSONObject["n4"] = value;

            // Convert back to a byte array
            var modBytes = Fiddler.WebFormats.JSON.JsonEncode(oJSON1.JSONObject);

            // Convert json to bytes, storing the bytes in request body
            var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
            oSession.responseBody = mod;

        }
    }

server has send the right JSON response

{"n4":"9pjjvezwRVUIvycpsXco4g==","eB":[92,684,"dLzPnLRYquJ7b7b"]}

which I captured in OnBeforeResponse function to modify and send it to the client.

in OnBeforeResponse function
var sbody = oSession.GetResponseBodyAsString();
gives me the right JSON response as above.

but when I am converting JSON back to byte array using

var modBytes = Fiddler.WebFormats.JSON.JsonEncode(oJSON1.JSONObject);

at this point my JSON response got reversed to

{"eB":[92,684,"dLzPnLRYquJ7b7b"], "n4":"9pjjvezwRVUIvycpsXco4g==1"}

I am not able to figure out what I am doing wrong. Please help me out. Completely new to Fiddler and JSON.

Eric Lawrence

unread,
Oct 26, 2017, 1:11:09 PM10/26/17
to Fiddler
The JSON standard does not impose ordering requirements on object properties, so they can appear in any order:

"An object is an unordered set of name/value pairs."

If the application requires that the properties appear in a particular order, it's not really using JSON. You could modify the response in Fiddler using string manipulation (avoiding the WebFormats.JSON object) if necessary.

sandeep goyal

unread,
Oct 26, 2017, 1:48:00 PM10/26/17
to Fiddler
Thanks Eric,

is there any link/function available on how to do string manipulation in Fiddler. As you see in my example I want to modify value of "n4"

Eric Lawrence

unread,
Oct 26, 2017, 2:34:06 PM10/26/17
to Fiddler
Before you go down the path of string manipulation, can you confirm that having the order of the value change breaks the client application?

In terms of string manipulation, you could simply grab the value of the "n4" node (e.g. by interpreting as a JSON object) and then do e.g.

   oSession.utilSetResponseBody( originalBodyString.Replace("oldtokenvalue", "newtokenvalue") );

sandeep goyal

unread,
Oct 26, 2017, 2:42:46 PM10/26/17
to Fiddler
Yes Eric, if key got reversed, I am receiving a particular error that client was expecting n4 but received eb.
one thing I noticed that if I didn't catch server response in OnBeforeResponse function and let it to go as it is to the client, key value never got reversed. it's getting reversed only when I capture the response in OnBeforeResponse function and again encode it using Fiddler.WebFormats.JSON.JsonEncode.

Eric Lawrence

unread,
Oct 26, 2017, 2:52:00 PM10/26/17
to Fiddler
> key value never got reversed.

Right. By default, Fiddler makes no changes to the content of requests or responses (with rare exceptions for malformed traffic). The problem in your scenario arises because Fiddler.WebFormats.JSON.JsonEncode follows the rules of JSON encoding, in which the order of object properties is not important. As a consequence, it makes no attempt to maintain the order of nodes, and this leads to the reordering you observed.


sandeep goyal

unread,
Oct 26, 2017, 3:31:41 PM10/26/17
to Fiddler
ok..

but I haven't seen any reordering issue when I modify the request sent by the client to the server. I modify the request using OnBeforeRequest function and encode it using
Fiddler.WebFormats.JSON.JsonEncode.

it means in OnBeforeRequest, Fiddler.WebFormats.JSON.JsonEncode. take care of the orders of the nodes.

but not in OnBeforeResponse.

Eric Lawrence

unread,
Oct 26, 2017, 3:41:25 PM10/26/17
to Fiddler
It's simply a coincidence that it turned out that way. Depending on the values of the data and/or other factors (the .NET framework implementation), the nodes can be reordered.

sandeep goyal

unread,
Oct 26, 2017, 4:15:15 PM10/26/17
to Fiddler
okkk..

I will go  down the path of string manipulation, as the function you suggested.
Can we perform other operation on this string like "

decode my "n4" to base64(not binary body), store it in byte array and corrupt the byte and then again encode it using base64. That would be a great help. Sorry I am not aware of these operations in fiddler.

Eric Lawrence

unread,
Oct 26, 2017, 4:42:19 PM10/26/17
to Fiddler
Fiddler can do all of these things, but keep in mind that you're basically writing C# or JScript.NET (FiddlerScript defaults to JScript.NET, but you can change it inside Tools > Fiddler Options).

The .NET Framework (used by either C# or JS.NET) offers methods to do base64 encoding/decoding:

  Convert.ToBase64String
  Convert.FromBase64String

For bit manipulation, you'll typically just operate on the bytes of the array, e.g.

  myBytes[5] = myBytes[5] & 0x010;

or whatever (I haven't bit twiddling with JS, only C#).

sandeep goyal

unread,
Oct 26, 2017, 5:50:44 PM10/26/17
to Fiddler
okk... I have shifted to c#

I have write just 3 line of code


public static void OnBeforeResponse(Session oSession) {
var oResponseBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
var oJSON = Fiddler.WebFormats.JSON.JsonDecode(oResponseBody);
var value = oJSON.JSONObject["n4"];
}

but I'm receiving compilation error at line var value = oJSON.JSONObject["n4"];
"object does not contain a definatin of JSONObject and no extension method 'JSONObject' accepting a first argument of type 'object' could be found"

I didn't see this compilation error while using JS.net. Am I missing any directive to use in c#?


Eric Lawrence

unread,
Oct 26, 2017, 6:19:38 PM10/26/17
to Fiddler
1. Don't convert to text manually. Use

   var oResponseBody = oSession.GetResponseBodyAsString();

instead.

2. I think the issue here is that JSONObject gets type-casted automagically in JS.NET but not in C#. try e.g.

  var oObj = oJSON.JSONObject as Hashtable;
 
  var oVal = (oObj != null) ? oObj["n4"] : null;


sandeep goyal

unread,
Oct 26, 2017, 6:54:24 PM10/26/17
to Fiddler
still the same compilation error, I  uses the below code:

string oResponseBody = oSession.GetResponseBodyAsString();
var oJSON = Fiddler.WebFormats.JSON.JsonDecode(oResponseBody);
var oObj = oJSON.JSONObject as Hashtable;        ----------------->Same compilation error here
var oVal = (oObj != null) ? oObj["n4"] : null;

I tried to used JSONParseResult too, but I received the compilation error of
"the type or namespace name 'JSONParseResult ' could not be found. are you missing a using directive or assembly reference"
see below code:
string oResponseBody = oSession.GetResponseBodyAsString();
Fiddler.WebFormats.JSON.JSONParseResult oJSON;
oJSON = (JSONParseResult)Fiddler.WebFormats.JSON.JsonDecode(oResponseBody);    ---->compilation error here

Eric Lawrence

unread,
Oct 27, 2017, 12:38:01 PM10/27/17
to Fiddler
At the top, add: 

   using Fiddler.WebFormats;

In the handler, add 

var oResponseBody = oSession.GetResponseBodyAsString();
JSON.JSONParseResult oJSON = JSON.JsonDecode(oResponseBody) as JSON.JSONParseResult;
Hashtable oObj = oJSON.JSONObject as Hashtable; 
Reply all
Reply to author
Forward
0 new messages