Using FiddlerScript (JScript.Net) to change JSON response to null

1.3k Views Asked by At

I am using Fiddler's "FiddlerScript" to modify responses from a web server so that I can test responses in my app.

Here is my OnBeforeResponse function:

static function OnBeforeResponse(oSession: Session) {
    // This code was already here, leaving it
    if (m_Hide304s && oSession.responseCode == 304) {
        oSession["ui-hide"] = "true";
    }
    // Here is new code to modify server's response
    if(oSession.HostnameIs("mydomain.com") && oSession.uriContains("config")) {
        // Color this response, so we can spot it in Fiddler
        oSession["ui-backcolor"] = "lime";

        // Convert the request body into a string
        var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

        var j: Fiddler.WebFormats.JSON.JSONParseResult;
        // Convert the text into a JSON object
        // In this case our JSON root element is a dictionary (HashTable)
        j = Fiddler.WebFormats.JSON.JsonDecode(oBody);

        // Inside of our dictionary, we have an array (ArrayList) called "placements"
        var testObject = j.JSONObject["test"];
        /* Change this to different values, e.g. "0.0", 0.0, null, "", etc. */
        /* This works */
        testObject["consent_version"] = "";
        /* This works */
        testObject["consent_version"] = 0.0;
        /* This works */
        testObject["consent_version"] = "0.0";
        /* This fails */
        testObject["consent_version"] = null;

        // 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.ResponseBody = mod;
    }
}

I am able to set testObject["consent_version"] to any value except null. If I set this to null, Fiddler creates JSON which doesn't have any value, and the JSON is in a bad format like this:

"consent_version":,

Note that there is no value after "consent_version" and before the comma.

Does anyone know how I can use FiddlerScript (which is based on JScript.Net) to set a null value?

1

There are 1 best solutions below

1
On

I know this is an old question, but I came across it trying to do the exact same thing, and finally figured it out... Hopefully this helps someone in the future.

First off, instead of setting it to null, set it to undefined. Chrome developer tools still shows it as returning as null.

Then, instead of

var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
var mod = System.Text.Encoding.UTF8.GetBytes(modBytes);
oSession.ResponseBody = mod;

Use:

var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
oSession.utilSetResponseBody(modBytes);

And you should be all set.

Here's a full example:

static function OnBeforeResponse(oSession: Session) {
    if (m_Hide304s && oSession.responseCode == 304) {
        oSession["ui-hide"] = "true";
    }
    
    if (true && (oSession.host=="domain.com") && (oSession.PathAndQuery.indexOf("/GetDataFromServer")>-1) && (oSession.oResponse.headers.ExistsAndContains("Content-Type", "json")) ){
        oSession["ui-bold"]="true";
        
        // Convert the request body into a string
        oSession.utilDecodeResponse();
        var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

        // FiddlerObject.log(oBody);
        
        var j: Fiddler.WebFormats.JSON.JSONParseResult;
        j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
        
        // FiddlerObject.log(j.JSONObject[0]["PropertyName"]);
        j.JSONObject[0]["PropertyName"] = undefined;
        
        // Convert back to a byte array
        var modBytes = Fiddler.WebFormats.JSON.JsonEncode(j.JSONObject);
        oSession.utilSetResponseBody(modBytes);
    }
}