首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并使用json response REST API读取响应头

并使用json response REST API读取响应头
EN

Stack Overflow用户
提问于 2015-07-05 16:38:00
回答 1查看 2.1K关注 0票数 0

我正在使用frisby自动化REST API测试。我所有的REST API都基于json并返回json响应。在其中一个需求中,我需要读取响应头并获取响应头,并将其设置为下一个请求。使用json响应时,我无法读取响应头。以下是我的测试的示例代码。

代码语言:javascript
复制
frisby.create("Check to make sure that user does exist")
                                            .get(hostURL + "/api/users/checkusername/" + username, user, {json: true}, {headers: {'Content-Type': 'application/json'}})
                                            .expectHeaderContains('content-type', 'application/json')
                                            .afterJSON(function (response) {
                                            //How to read session id from header
                                                //var sessionId = res.headers[constants.SESSION_ID_HEADER_KEY]; 
                                                var exist = response.exist;
                                                expect(exist).toBe(true);

                                                });

请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2015-07-09 10:19:42

你的代码实际上是好的,你只是试图使用'res‘变量而不是响应。

代码语言:javascript
复制
frisby.create("Check to make sure that user does exist")
.get(hostURL + "/api/users/checkusername/" + username, user, {json: true}, {headers: {'Content-Type': 'application/json'}})
.expectHeaderContains('content-type', 'application/json')
.afterJSON(function (response) {
  var sessionId = response.headers[constants.SESSION_ID_HEADER_KEY]; 
  // Use the sessionId in other frisby.create(...) call
}).
toss();

另一种选择是使用after(),如下所示:

代码语言:javascript
复制
frisby.create("Check to make sure that user does exist")
.get(hostURL + "/api/users/checkusername/" + username, user, {json: true}, {headers: {'Content-Type': 'application/json'}})
.expectHeaderContains('content-type', 'application/json')
.after(function (err, res, body) {
  var obj = JSON.parse(body);
  var sessionId = obj.headers[constants.SESSION_ID_HEADER_KEY]; 
  // Use the sessionId in other frisby.create(...) call
}).
toss();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31228561

复制
相关文章

相似问题

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