首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fiddler:无法在

Fiddler:无法在
EN

Stack Overflow用户
提问于 2018-08-03 17:51:22
回答 1查看 319关注 0票数 0

我们使用Fiddler customRules.js脚本来处理我们的API测试(当其他公司没有测试服务器时来自它们的外部API),其中如果有一个响应文件,我们会将一个响应文件发送回请求者,否则我们构建响应。这很好,但是我不能设置HTTP状态代码。

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 21:11:45

终于找到了。问题是,当使用oSession["x-replywithfile"]返回错误时,我经常返回一个文件。但是,这总是使状态成为200 OK,即使我试图在oSession["x-replywithfile"]设置之后更改状态。

代码语言:javascript
复制
oSession["x-replywithfile"] = ReturnFileName;
oSession.responseCode = 500;

这仍然将始终返回一个200 OK

更改为下面的内容将有效。

代码语言:javascript
复制
var FileContents = ReadFile(ReturnFileName);
oSession.utilSetResponseBody(FileContents);
oSession.responseCode = 500;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51677783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档