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

Fiddler: Cannot set the Response Code in

我们正在使用 Fiddler customRules.js 脚本来处理我们的 API 测试(来自其他公司的外部 APIs,当他们没有为我们提供测试服务器时)我们发送一个如果存在响应文件返回给请求者,否则我们构建响应。这工作正常,但我无法设置 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;
}

终于找到了。问题是我经常 returning 文件时 returning 使用 oSession["x-replywithfile"] 出错。但是,这总是使状态成为 200 OK,即使我尝试在 oSession["x-replywithfile"] 设置后更改状态。

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

这将始终 return 一个 200 OK

更改为以下内容即可。

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