我们使用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;
}发布于 2018-08-08 21:11:45
终于找到了。问题是,当使用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;https://stackoverflow.com/questions/51677783
复制相似问题