简体   繁体   English

阅读REST POST响应node.js

[英]reading REST POST response node.js

I am able to test a post call in rest client which shows headers as: 我可以在rest客户端测试一个post调用,它显示标题为:

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

my code to invoke the post is as follows: 我调用帖子的代码如下:

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

However i get the printed stuff: 但是我得到印刷的东西:

the decoded data is: undefined

var http = require("http"), zlib = require("zlib"); var http = require(“http”),zlib = require(“zlib”);

function getGzipped(url, callback) { // buffer to store the streamed decompression var buffer = []; function getGzipped(url,callback){//用于存储流式解压缩var buffer = []的缓冲区;

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, function(err, data) { console.log(data); }); getGzipped(url,function(err,data){console.log(data);});

You are printing the response body in your code, not the response headers. 您正在代码中打印响应正文,而不是响应标头。

Try this code in order to see the headers: 尝试使用此代码以查看标题:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM