json - 使用 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)来设置空值?
解决方案
我知道这是一个老问题,但我在尝试做完全相同的事情时遇到了它,最后想通了......希望这对未来的人有所帮助。
首先,不要将其设置为 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);
}
}
推荐阅读
- javascript - React Leaflet:调用但未执行的函数
- axios - 如何通过 axios 或 ajax 请求通过 API 将图像发送到 Google Form?
- c# - 在 ASP.NET Core 中启动 BackgroundService 的正确方法
- c# - 删除 ASP.NET Core 中的 http 客户端日志记录处理程序
- javascript - 将 url 分配给 Promise 内的 img 源后,不会触发加载事件侦听器
- javascript - 如何处理从 javascript(react-native)到 PHP 的 fetch 调用中的对象数组?
- php - 为什么我的 $_SESSION 会在文件之间丢失多维数组数据?
- javascript - 使用不同的参数调用函数两次
- fiware - 我怎么能用 Fiware-Perseo-fe 做下面的例子
- java - DJI Lightbridge 2 的视频源损坏