HPE_INVALID_HEADER_TOKEN,同时尝试使用Express JS路由解析API响应

时间:2019-06-12 03:19:32

标签: javascript node.js api express parsing

我试图从Postman调用一个API,该API会迅速给出响应,但是当我尝试使用Node.js Express路由做同样的事情时,出现以下错误,并且下面是我的代码:

var express = require('express');
var router = express.Router();
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
var request = require("request");

var options = { method: 'GET',
    url: 'https://test.dev.api.google.io/api/isc/fhir/stu3/Observation',
    qs:
        { _format: 'json',
            patient: '434343',
            code: '432222',
            date: 'gt2019-06-11' },
    headers:
        { 'cache-control': 'no-cache',
            Connection: 'keep-alive',
            contentType: 'application/json',
            'accept-encoding': 'gzip, deflate',
            cookie: 'visid_incap_1839367=ajslkjaljfaljfa/jfalskjalf;incap_ses_1172_1839367=saljfalsjlakjfssalkjfalkjal==; incap_ses_148_1839367=saljlajflakjfkaljfal+ajslajlsfsajlakjalfjalj==; incap_ses_703_1839367=alsjlajslfkajlsakj/sjlasjlajfsa+m5p+vFgBJO0w==; incap_ses_702_1839367=jlajslfjaljfa+jalskjla/PpBN7BSv7mFcjhQ8pw==',
            Host: 'call.dev.api.google.io',
            'Postman-Token': '84kdidfjsaflasjflakjfalfa',
            'Cache-Control': 'no-cache',
            Accept: '*/*',
            'User-Agent': 'PostmanRuntime/7.13.0',
            'x-google-version': '1.0',
            'Ocp-Apim-Subscription-Key': 'sflaalkjflakjfsla' } };


/* GET users listing. */
router.get('/', function(req, res, next) {

    request(options, function (error, response, body) {
        if (error) throw new Error(error);

        console.log(body);
    });

    res.send("receigsdgsgved...");

});

module.exports = router;


**this is the error:**                                                  




**error = Error: Parse Error
        at TLSSocket.socketOnData (_http_client.js:460:22)
        at TLSSocket.emit (events.js:200:13)
        at addChunk (_stream_readable.js:290:12)
        at readableAddChunk (_stream_readable.js:271:11)
        at TLSSocket.Readable.push (_stream_readable.js:226:10)
        at TLSWrap.onStreamRead (internal/stream_base_commons.js:166:17) {bytesParsed: 911,
    code: "HPE_INVALID_HEADER_TOKEN",
    reason: "Invalid header value char",
    stack: "Error: Parse Error\n    at TLSSocket.socketOnData (…reamRead (internal/stream_base_commons.js:166:17)",
    message: "Parse Error"}
     bytesParsed = 911
     code = "HPE_INVALID_HEADER_TOKEN"
     message = "Parse Error"
     reason = "Invalid header value char"
     stack = "Error: Parse Error\n    at TLSSocket.socketOnData (_http_client.js:460:22)\n    at TLSSocket.emit (events.js:200:13)\n    at addChunk (_stream_readable.js:290:12)\n    at readableAddChunk (_stream_readable.js:271:11)\n    at TLSSocket.Readable.push (_stream_readable.js:226:10)\n    at TLSWrap.onStreamRead (internal/stream_base_commons.js:166:17)"**

我不确定可能出了什么问题,感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在尝试在此处创建多行字符串:

            cookie: 'visid_incap_1839367=ajslkjaljfaljfa/jfalskjalf; 
            incap_ses_1172_1839367=saljfalsjlakjfssalkjfalkjal==; incap_ses_148_1839367=saljlajflakjfkaljfal+ajslajlsfsajlakjalfjalj==; incap_ses_703_1839367=alsjlajslfkajlsakj/sjlasjlajfsa+m5p+vFgBJO0w==; incap_ses_702_1839367=jlajslfjaljfa+jalskjla/PpBN7BSv7mFcjhQ8pw==',

这必须是一行:

            cookie: 'visid_incap_1839367=ajslkjaljfaljfa/jfalskjalf; incap_ses_1172_1839367=saljfalsjlakjfssalkjfalkjal==; incap_ses_148_1839367=saljlajflakjfkaljfal+ajslajlsfsajlakjalfjalj==; incap_ses_703_1839367=alsjlajslfkajlsakj/sjlasjlajfsa+m5p+vFgBJO0w==; incap_ses_702_1839367=jlajslfjaljfa+jalskjla/PpBN7BSv7mFcjhQ8pw==',