2017-07-03 135 views
1

如何从http获取init响应?基本上我的代码是这个,但我无法达到这些对象。从http获取请求的响应请求

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... 
+0

你对此有何回应返回? – evolutionxbox

+0

我认为你正在尝试访问的是正文,正文是包含从端点返回的实际数据的正文。响应是一个http.IncomingMessage对象(Response对象)。你可以阅读更多关于它在这里https://nodejs.org/api/http.html#http_class_http_incomingmessage –

+0

我想达到一个初始化响应,可能有一些数据,我dk,我怎么能找到一个数组数据响应 – user8198887

回答

2

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

你可以从body PARAM访问结果,这样

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. 
});