【问题标题】:GET rest call in Node.js not showing output in JSONNode.js 中的 GET 休息调用未在 JSON 中显示输出
【发布时间】:2016-07-11 06:10:41
【问题描述】:

我在 node.js 中编写了一个 GET 休息调用。我的 post 调用工作正常,但是当我使用 get 调用时,我看不到 Json 格式的输出。事实上,我看到一些特殊字符的输出。请指教我哪里出错了。

var http = require('http');
var requestrespone = require("request");
var config = require('../config/config.js');
var jsonFile = require('jsonfile');
var jsonFile=require('../json_input/jsoninput.json');
var cheerio =require('cheerio');
var express=require('express');
var app=express();
var postOptions = {
    host : config.host,
    method : config.post,
    path: config.path,
    headers :config.postheader
};


    var getOptions=
    {
        host : config.host,
        method : config.get,
        path: config.getpath,
        headers :config.getheader

    };



jsonObject = JSON.stringify(jsonFile);


console.info('Options prepared:');
console.info(postOptions);
console.info('Starting the POST call');


var jsonString;
function mapFeedbackIdGeneration(done)
{

    var reqPost = http.request(postOptions, function(res) {
    console.log("statusCode: ", res.statusCode);
    res.on('data', function(data) {
    jsonString=JSON.parse(data);
    return done(jsonString);
      });

    res.on('close', function() { 
    });
});


reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
console.error(e);
});}
///----------GET CALL----------------->>>>>
console.info('Options prepared:');
console.info(getOptions);

console.info('Do the GET call');

var options = {
  "method": "GET",
  "hostname": "maphub.cit.api.here.com",

  "path": "/feedback/-936624",
  "headers": {

    'Accept-Encoding':'gzip,deflate',
    'Authorization':'AWS O7Qx6dyYEnwwBT2Hn5Es:gR/rjtlGsggWEZ1ItPOw0oGXdPM=',

'Auth-Identifier':'Y-7Wnc2lNk3fMI0n3-rZ',

'Auth-Service-Id':'here_app',
'Group-Id':'FGx1AWaAzKOo0imNkLmf',
'Auth-Secret':'mEv_DZ1JKDDYzESUAp9KyQ',
'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0',

'Host':'maphub.cit.api.here.com'


}
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
      console.log("chunks data");
      console.log(chunks);
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log("hello"+body);
  });
});

req.end();
module.exports.mapFeedbackIdGeneration=mapFeedbackIdGeneration;

//module.exports.getMapfeedback=getMapfeedback;

我的输出应该是 JSON 数据,但它显示:

Do the GET call
chunks data
[]
hello▼       T?AO?0►????8?‼?"?*??j↨q@=L?i?%???¶§?⌂?I`w?↨?~??,???        ?.♀???,?
ZV?{m@W?Q↕[?F?}WCg?Y☼↨L?1??^?p??=?m?{?>?????▼?????l^P↨?JQTUSs????T<?????f??K1Su?
?4??KQ??
)UiA
"?X(D?PKA‼G????????KD?????j>X?p?`??%$ ?▼^!
?i/!b?n??`?!∟???=?4g?B?????j?yXln?[??)?D▲?wzIj?@?¶[N?4F}u       ??]?d\=?n?{▲3??n
►???♥??7?‼?♂??\G?S$?♠2YF{?♥>n??5m?▼???/? ??q?y}??>)??????♫??\????7   ??♥ ☼??↑☻

statusCode:  200

我哪里错了?

【问题讨论】:

  • 尝试从获取选项中删除'Accept-Encoding':'gzip,deflate',

标签: javascript json node.js rest


【解决方案1】:

您的代码有问题。删除 options 变量中的以下内容。

'Accept-Encoding':'gzip,deflate',

上面一行告诉远程服务器客户端可以接受gzip压缩文件并且可以解压它。但是,您的应用程序不执行解压缩部分。因此,您看到的是乱码数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 2013-04-15
    • 2013-12-13
    • 2022-08-13
    • 2020-03-14
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多