Javascript 访问 request-promise 中的 headers 得到响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39234359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:21:09  来源:igfitidea点击:

Access to headers in request-promise get response

javascriptpromise

提问by learningtocode

I am complete newbie to JS world. I am trying to write a test case that tests user's actions on a site. I am using request-promise module to test the asyn calls. I could not find any api documentation for request-promise. How do I get access to status code of the response? Right now it prints undefined. Also, can anyone please confirm, how do we know what promise returns when it is successful, is it a single value that it resolves to or all the parameters that the async function returns. How do we know what are the parameters to function() in request.get(base_url).then(function(response, body).

我是 JS 世界的完全新手。我正在尝试编写一个测试用例来测试用户在站点上的操作。我正在使用 request-promise 模块来测试异步调用。我找不到任何请求承诺的 api 文档。如何访问响应的状态代码?现在它打印未定义。另外,任何人都可以确认一下,我们如何知道 promise 在成功时返回的内容,它是解析为单个值还是异步函数返回的所有参数。我们怎么知道request.get(base_url).then(function(response, body).

var request = require("request-promise");
var promise = require("bluebird");
//
var base_url = "https://mysignin.com/"
//
describe("My first test", function() {
 it("User is on the sign in page", function(done) {
    request.get(base_url).then(function(response, body){
     // expect(response.statusCode).toBe('GET /200');
      console.log("respnse " + response.statusCode);
      console.log("Body " + body);
      done();
    }).catch(function(error) {
        done("Oops somthing went wrong!!");
    });
 });
});

回答by Rob Bailey

By default, the request-promise library returns only the response itself. You can, however, pass a simple transform function in your options, which takes three parameters and allows you to return additional stuff.

默认情况下,请求承诺库只返回响应本身。但是,您可以在选项中传递一个简单的转换函数,它接受三个参数并允许您返回其他内容。

So if I wanted the headers plus the response returned to me, I would just do this:

所以如果我想要标题加上响应返回给我,我会这样做:

var request = require('request-promise');
var uri = 'http://domain.name/';

var _include_headers = function(body, response, resolveWithFullResponse) {
  return {'headers': response.headers, 'data': body};
};

var options = {
  method: 'GET',
  uri: uri,
  json: true,
  transform: _include_headers,
}

return request(options)
.then(function(response) {
  console.log(response.headers);
  console.log(response.data);
});

Hope that helps.

希望有帮助。

回答by Kostas

By default request-promisereturns just the response body from a request. To get the full response object, you can set resolveWithFulLResponse: truein the options object when making the request. Example in the docs

默认情况下,request-promise只返回请求的响应正文。要获得完整的响应对象,您可以resolveWithFulLResponse: true在发出请求时在选项对象中进行设置。文档中的示例

var request = require('request-promise');

request.get('someUrl').then(function(body) {
  // body is html or json or whatever the server responds
});

request({
  uri: 'someUrl',
  method: 'GET',
  resolveWithFullResponse: true
}).then(function(response) {
  // now you got the full response with codes etc...
});

回答by thilak

Just passing resolveWithFullResponse: truewith in the get options should fetch the response headers.

只需resolveWithFullResponse: true在 get 选项中传递即可 获取响应标头。

回答by solendil

Tsalikidis answer is correct. As for:

Tsalikidis 的回答是正确的。至于:

Also, can anyone please confirm, how do we know what promise returns when it is successful, is it a single value that it resolves to or all the parameters that the async function returns

另外,任何人都可以请确认,我们如何知道成功时返回的promise是它解析的单个值还是异步函数返回的所有参数

A promise (Promise/A+ compliant) always return one single value. Of course this value can be a deeply nested object with tons of information in it. But .then(function(response,body){is inherently wrong.

一个承诺(Promise/A+ 兼容)总是返回一个单一的值。当然,这个值可以是一个深度嵌套的对象,其中包含大量信息。但.then(function(response,body){本质上是错误的。

A library that sends back a promise should document the format of the returned object.

发回承诺的库应该记录返回对象的格式。