使用 FiddlerScript 的 OnBeforeResponse 更改数组中元素的属性
Use FiddlerScript's OnBeforeResponse to change properties of elements in an array
我正在尝试编写一个 FiddlerScript 来修改从服务器返回的 JSON 数组的属性。
这是我目前尝试过的方法:
static function OnBeforeResponse(oSession: Session) {
if (m_Hide304s && oSession.responseCode == 304) {
oSession["ui-hide"] = "true";
}
if(oSession.HostnameIs("myserver.com") && oSession.uriContains("info")) {
oSession["ui-backcolor"] = "lime";
// 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);
var placementsArray = j.JSONObject["placements"];
FiddlerObject.log("Got Placements.");
// I can't figure out how to access the elements of the array
//for (var i: int=0; i < placementsArray.Length; i++) {
// placements[i]["isValid"] = true;
//}
// 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;
}
}
我需要有关此函数中间被注释掉的 for 循环的帮助。我想遍历 "placements" 的数组,然后在每个 "placement" 对象中更改一个名为 "IsValid" 的值。我需要这样做,以便我可以修改来自服务器的响应,以便我可以使用数组项 属性 值的不同服务器响应来测试客户端应用程序。
这里是答案,以防对任何人有帮助。我错误地获取和设置了 Request 主体而不是 Response 主体,而且我还使用了不存在的 "Length" 属性 的 ArrayList 而不是 "Count".
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("myserver.com") && oSession.uriContains("info")) {
// 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 placementsArray = j.JSONObject["placements"];
for (var iEach = 0; iEach < placementsArray.Count; iEach++){
// In each object/member in the array, we change one of its properties
placementsArray[iEach]["isValid"] = true;
}
// 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;
}
}
我正在尝试编写一个 FiddlerScript 来修改从服务器返回的 JSON 数组的属性。
这是我目前尝试过的方法:
static function OnBeforeResponse(oSession: Session) {
if (m_Hide304s && oSession.responseCode == 304) {
oSession["ui-hide"] = "true";
}
if(oSession.HostnameIs("myserver.com") && oSession.uriContains("info")) {
oSession["ui-backcolor"] = "lime";
// 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);
var placementsArray = j.JSONObject["placements"];
FiddlerObject.log("Got Placements.");
// I can't figure out how to access the elements of the array
//for (var i: int=0; i < placementsArray.Length; i++) {
// placements[i]["isValid"] = true;
//}
// 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;
}
}
我需要有关此函数中间被注释掉的 for 循环的帮助。我想遍历 "placements" 的数组,然后在每个 "placement" 对象中更改一个名为 "IsValid" 的值。我需要这样做,以便我可以修改来自服务器的响应,以便我可以使用数组项 属性 值的不同服务器响应来测试客户端应用程序。
这里是答案,以防对任何人有帮助。我错误地获取和设置了 Request 主体而不是 Response 主体,而且我还使用了不存在的 "Length" 属性 的 ArrayList 而不是 "Count".
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("myserver.com") && oSession.uriContains("info")) {
// 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 placementsArray = j.JSONObject["placements"];
for (var iEach = 0; iEach < placementsArray.Count; iEach++){
// In each object/member in the array, we change one of its properties
placementsArray[iEach]["isValid"] = true;
}
// 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;
}
}