首页 > 解决方案 > 将修改后的请求发送到服务器的 Fiddlerscript 不起作用

问题描述

我创建了一个 fiddlescript 规则,我认为它会等待特定的对象:值,自动将 json 值的一部分发送回不同的 URI,作为具有相同标头信息(如 cookie)的发布请求。

当脚本被激活时,我收到一个错误。我认为它与 json 对象值有关。

    static function OnBeforeResponse(oSession: Session) {
        if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "application/json")) {
            oSession["ui-backcolor"] = "blue"; 
            oSession.utilDecodeResponse();
        }
        if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "application/json") && oSession.utilFindInResponse("faceId", false) > -1) {
            oSession["ui-backcolor"] = "green"; 
            oSession.utilDecodeResponse();
            var oBody = System.Text.Encoding.UTF8.GetString(oSession.requestBodyBytes);
            var j = Fiddler.WebFormats.JSON.JsonDecode(oBody);
            var facId = j.JSONObject["faceId"];
            var reqBod = '{"faceId":"' + facId + '"}';
            oSession.oRequest.headers.HTTPMethod == "POST";
            oSession.utilSetRequestBody(reqBod);
            oSession.url = "https://urltosendpostrequest.com/Search";
            FiddlerObject.utilIssueRequest(oSession);
        }

我希望服务器接受修改后的 POST 请求,但脚本却出错了。

FiddlerScript OnBeforeResponse0 failed. X 
There was a problem with your FiddlerScript. 
Function expected Function expected at Microsoft.)Script.Latainding.CallValue(Object val, Objects arguments, Boolean construct, Boolean brackets, VsaEngine engine, Object thisob, Binder binder, Culturelnfo culture, Strings namedParameters) at Microsoft.JScript.Latainding.Call(Binder binder, Objects arguments, ParameterModifier]] modifiers, Culturelnfo culture, Strings namedParameters, Boolean construct, Boolean brackets, VsaEngine engine) at Microsoft.JScript.Latainding.Call(ObjectS arguments, Boolean construct, Boolean brackets, VsaEngine engine) at Fiddler.ScriptNamespace.Handlers.OnBeforeResponse(Session oSession) at Fiddler.ScriptBase. 1:1(Session OD) in CA.lenkins\Fiddler_Windows\workspace\Fiddler2\Common\Application\ Scripting\ScriptBase.csiline 921 

标签: javascriptjsonhttppostfiddler

解决方案


您正在混合访问请求和响应标头/正文。此外,您似乎不清楚POST数据属于请求而不是响应。因此,请确保您正在处理正确的数据(请求或响应)。

还是您想捕获请求并将自定义响应发送回客户端?如果是,您应该查看AutoResponder Flagx-replywithfile

您的脚本中有代码,OnBeforeResponse因此您只能访问与响应相关的属性和方法。但是,您有以下代码尝试访问请求(这当然是不可能的,因为请求已经被转发):

oSession.oRequest.headers.HTTPMethod == "POST";
oSession.utilSetRequestBody(reqBod);
FiddlerObject.utilIssueRequest(oSession);

推荐阅读