代码之家  ›  专栏  ›  技术社区  ›  Steven Scott

Fiddler:无法在中设置响应代码

  •  0
  • Steven Scott  · 技术社区  · 8 年前

    我们正在使用Fiddler customRules.js脚本来处理我们的API测试(当其他公司没有测试服务器时来自外部API),在那里,如果存在响应文件,我们会将其发送回请求程序,否则我们会生成响应。这工作正常,但我无法设置HTTP状态代码。

    在生成响应时,在某些情况下,我们希望能够将HTTP状态指定为外部API可能发送的内容。

    static function OnBeforeResponse(oSession: Session) {
        if (m_Hide304s && oSession.responseCode == 304) {
            oSession["ui-hide"] = "true";
        } 
    
        // Set Header values for later
        var HeaderContentType = 'text/xml;charset=utf-8';
        var HeaderServer = 'Apache-Coyote/1.1';
        var HttpStatus = 200;
    
        ...  // This is the removed code that determines text or file to return
    
        // At the end of our process to determine to send a file or error we try to send an error value in this case. For simplicity, I am just hard assigning it without using a variable as we normally would. 
        oSession.responseCode = 500;
        oSession.oResponse.headers.HTTPResponseCode = 500;
        oSession.oResponse.headers.HTTPResponseStatus = "500 SERVER ERROR";
    
        oSession.ResponseHeaders.SetStatus(500, 'Server Error');   // This also does not work
    
        // However this does work to add the file contents into the response when the file exists.
        var ResponseFile = new ActiveXObject("Scripting.FileSystemObject");
        if (ResponseFile.FileExists(ReturnFileName)) {
            oSession["x-replywithfile"] = ReturnFileName;
    
            // Error message returned as the ReturnBody was not populated and Response File not found
        } else {
            oSession.utilSetResponseBody(ErrorMessage);
        }
    
        return;
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Steven Scott    8 年前

    终于找到了。问题是,当使用 oSession["x-replywithfile"] . 然而,这总是使状态 200 OK ,即使我尝试在 操作会话[“x-replywithfile”] 设置。

    oSession["x-replywithfile"] = ReturnFileName;
    oSession.responseCode = 500;
    

    这仍然会返回 200好 .

    更改为以下将起作用。

    var FileContents = ReadFile(ReturnFileName);
    oSession.utilSetResponseBody(FileContents);
    oSession.responseCode = 500;