2017-04-14 107 views
1

我能够测试在客户休息后调用,它可以显示页眉为:阅读REST POST响应的node.js

Cache-Control: no-cache, no-store, must-revalidate 
Connection: keep-alive 
Content-Encoding: gzip 
Content-Language: en 
Content-Length: 49 
Content-Type: application/json 
Date: Fri, 14 Apr 2017 21:05:42 GMT 

我的代码来调用后如下:

var https = require('https'); 
... var jsonObject = JSON.stringify({ 
      "Name" : optyName 
     }); 

     // prepare the header 
     var postheaders = { 
      'Content-Type' : 'application/vnd.oracle.adf.resourceitem+json', 
      'authorization' : 'Basic am9obi5kdW5iYXI6dnBoODk1ODM=', 
      gzip: true 
     }; 

     // the post options 
     var optionspost = { 
      host : 'myhost', 
      port : 443, 
      path : '/salesApi/resources/11.1.11/opportunities?fields=OptyId&onlyData=true', 
      method : 'POST', 
      headers : postheaders, 
     }; 

     // do the POST call 
     var reqPost = https.request(optionspost, function(error,response,body) { 
      console.log('the decoded data is: ' + body) 
     }); 

    reqPost.write(jsonObject); 
     reqPost.end(); 
} 

但是我得到的印刷材料:

the decoded data is: undefined 

回答

0

变种HTTP =需要( “HTTP”), ZLIB =需要( “ZLIB”);

功能getGzipped(URL,回调){// 缓冲器来存储流传输的解压缩 变种缓冲= [];

http.get(url, function(res) { 
    // pipe the response into the gunzip to decompress 
    var gunzip = zlib.createGunzip();    
    res.pipe(gunzip); 

    gunzip.on('data', function(data) { 
     // decompression chunk ready, add it to the buffer 
     buffer.push(data.toString()) 

    }).on("end", function() { 
     // response and decompression complete, join the buffer and return 
     callback(null, buffer.join("")); 

    }).on("error", function(e) { 
     callback(e); 
    }) 
}).on('error', function(e) { 
    callback(e) 
}); 

}

getGzipped(URL,函数(ERR,数据){ 的console.log(数据); });

0

您正在代码中打印响应正文,而不是响应头。

请按顺序尝试这个代码,看看标题:

var reqPost = https.request(optionspost, function(error,response,body) { 
    console.log('response headers: ' + response.getHeaders()) 
}); 

https://nodejs.org/api/http.html#http_response_getheaders