小提琴手:无法在中设置响应代码

时间:2018-08-03 17:51:22

标签: fiddler

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

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

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 个答案:

答案 0 :(得分:0)

最后找到了它。问题是使用oSession["x-replywithfile"]返回错误时,我经常返回文件。但是,即使我尝试在200 OK设置后更改状态,这也始终使状态为oSession["x-replywithfile"]

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

这仍将始终返回200 OK

更改为以下内容即可。

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