gpt4 book ai didi

javascript - 读取 REST POST 响应 node.js

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:06 26 4
gpt4 key购买 nike

我可以在休息客户端中测试后调用,其 header 显示为:

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

最佳答案

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

函数 getGzipped(url, 回调) { //存储流式解压的缓冲区 var 缓冲区 = [];

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, 函数(错误, 数据) { 控制台.log(数据);});

关于javascript - 读取 REST POST 响应 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com