【问题标题】:NodeJS Request: How to get the response as a string?NodeJS请求:如何以字符串形式获取响应?
【发布时间】:2017-09-25 00:13:55
【问题描述】:

我有点迷路了。是否可以在.on(response) 中将响应正文作为字符串获取(见下文)?还是我疯了?我知道我可以.pipeWriteStream 的响应,但是我需要将响应正文和响应 statusCode 放在同一个地方才能继续我的代码。谢谢!

var request = require("request");
request(url)
.on('response', function(response) {
    console.log(response.statusCode); // 200
    console.log(/* I need to get response body string here */); // <--- can I have the response body as a string here?
})
.on("error", function(err){
    console.log("Problem reaching URL: ", err);
});

【问题讨论】:

    标签: node.js express node-request


    【解决方案1】:

    我会发表评论,但我不能。

    与在 npm 页面中一样(考虑到您不需要管道),您可以将正文与响应一起放在::

    var request = require("request");
    
    request(url, function (error, response, body) {
      console.log('error:', error); 
      console.log('statusCode:', response && response.statusCode); 
      console.log('body:', body); 
    }); 
    

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多