首页 > 解决方案 > 使用 FiddlerScript (JScript.Net) 将 JSON 响应更改为 null

问题描述

我正在使用 Fiddler 的“FiddlerScript”来修改来自 Web 服务器的响应,以便我可以在我的应用程序中测试响应。

这是我的 OnBeforeResponse 函数:

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

我可以将 testObject["consent_version"] 设置为除 null 之外的任何值。如果我将其设置为 null,Fiddler 会创建没有任何值的 JSON,并且 JSON 格式不正确,如下所示:

"consent_version":,

请注意,“consent_version”之后和逗号之前没有值。

有谁知道我如何使用 FiddlerScript(基于 JScript.Net)来设置空值?

标签: jsonfiddlerjscript.net

解决方案


我知道这是一个老问题,但我在尝试做完全相同的事情时遇到了它,最后想通了......希望这对未来的人有所帮助。

首先,不要将其设置为 null,而是将其设置为未定义。Chrome 开发人员工具仍将其显示为返回 null。

然后,而不是

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

利用:

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

你应该准备好了。

这是一个完整的例子:

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);
    }
}

推荐阅读