javascript - 读取来自 http get 请求的响应

标签 javascript node.js

我如何从 http get 读取初始响应?基本上我的代码是这样的,但我无法访问这些对象。

var request = require("request")
request({
    url: "http://xxx/xxx/get.php?act=init&aparam=parame",
    method:"get",
     json: true
}, function (error, response, body) {
console.log(response.toJSON);// why i cant reach every object etc from here like this




   });

响应是这样的

IncomingMessage {
  _readableState: 
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: BufferList { head: null, tail: null, length: 0 },
     length: 0,
     pipes: null,
 },.../// continues...

最佳答案

来自 https://www.npmjs.com/package/request

你可以从 body 参数访问结果,像这样

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred 
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
  console.log('body:', body); // Print the HTML for the Google homepage. 
});

关于javascript - 读取来自 http get 请求的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882245/

相关文章:

javascript - Internet Explorer 8 兼容模式下的 YouTube api 错误

javascript - 如何在 promise 拒绝时停止执行?

javascript - api获取请求,处理404

javascript - 无法获取要清除的文件类型输入

javascript - 如何在 JavaScript 字符串中编译 Ember Action ?

javascript - 合并和比较

node.js - nodeJS文件上传服务器不换线

javascript - 在 AdonisJS 中使用 .save() 函数时超出最大调用堆栈大小错误

node.js - Socket.io 配置错误

javascript - NPM MSSQL - 错误 : uncaughtException: Cannot read property 'release' of null