【问题标题】:Reading incoming HTTPS headers from Spring Boot 2 response with node.js使用 node.js 从 Spring Boot 2 响应中读取传入的 HTTPS 标头
【发布时间】:2018-10-31 04:47:39
【问题描述】:

在这个例子的下面,在变量'obj'中我得到了响应体。如何使用此 https node.js 库获取响应的标头值?

var options = {
    hostname: hostname,
    port: port,
    path: pathMethod,
    method: method,
    headers: {
        'Content-Type': APPLICATION_JSON,
        'Authorization': BEARER + localStorage.jwtToken
    },
    rejectUnauthorized: false,
    agent: false,
    requestCert: false

};

return new Promise(function(resolve, reject) {
var req = https.request(options, function(res) {
        res.setEncoding(ENCODING_UTF8);
        res.on('data', function(result) {

            try {
                const obj = JSON.parse(result);
                resolve({ 'httpStatus': PAGE_STATUS_200, 'result': obj });
            }
            catch(error) {
                console.error(error);
                resolve(resolve({ 'httpStatus': PAGE_STATUS_500 }));
            }

        });

        res.on('end', () => {
            console.log('No more data in response.');
        });
    });

    req.on('error', function(err) {
        console.log(`problem with request: ${err.message}`);
        reject(err);
    });

    if (postData) {
        req.write(postData);
    }

    req.end();
});

在我的浏览器中,我得到了所有必要的标题。我无法使用 https node.js lib 获取标头可能是什么问题?

【问题讨论】:

  • res.headers 将包含响应标头。
  • 我已经尝试过您的解决方案,但与我看到 15 个参数(包括 JWT 令牌)的 Chrome 浏览器相比,只得到了 4 个参数。还尝试使用 Postman 请求并在响应标头中获得相同的 11 个参数。但我需要获取一个使用 https node.js lib 无法获取的适当参数(JWT 令牌)。
  • 大家好。这是这个问题的答案。 github.com/infinitered/apisauce/issues/110stackoverflow.com/questions/37897523/… 简而言之,问题出在 Spring Boot 2 的后端。

标签: javascript node.js https http-headers response


【解决方案1】:

响应头应该在 res.headers 对象中可用,例如

// Log headers
console.log('Headers: ', res.headers);

见:https://nodejs.org/api/https.html

例如

const https = require ('https');


// This will return the IP address of the client
var request = https.request({ hostname: "httpbin.org", path: "/ip" },  (res) => {
    console.log('Headers: ', res.headers);
    res.on('data', (d) => {
        console.log('/ip response: ', d.toString());    
    });
});

// 也尝试使用请求库

var request = require('request');

var options = {
    url: "https://httpbin.org/ip",
    method: "get"
};

console.log('Requesting IP..');
request(options, function (error, response, body) {
    if (error) {
        console.error('error:', error);
    } else {
        console.log('Response: Headers:', response && response.headers);
    }
});

【讨论】:

  • 同一种解释。这也很好。 +1 对您的回答。 :)
  • 与原问题的解释相同,这显然是重复的。
  • 按照你的建议做了,但在代码的答案中只有 4 个参数,而浏览器中有 15 个。就我而言,我需要所有参数。
  • 您是否尝试过为此目的使用 curl?例如卷曲 -v 网址(卷曲 httpbin.org/ip -v)。令人惊讶的是,当从两个不同的来源调用相同的 url 时,我们得到的标题更少。你能告诉我们你在浏览器和 node.js 中得到的标题吗?
  • 我使用 Postman 发送了请求,并在响应标头中获得了相同的 11 个参数。但我需要使用 https node.js lib 从代码中获取至少 11 个参数。
【解决方案2】:

您可以在 https 模块中获取标头。

这就是您获取响应的标头的方式。

res.headers

我在下面的示例中更新了您的代码:

    var req = https.request(options, function(res) {
    res.setEncoding(ENCODING_UTF8);
    res.on('data', function(result) {

    console.log("Headers: ", res.headers);

    // Your code here.

    });

    res.on('end', () => {
    // Do something here.
    });
});

希望这会有所帮助。

【讨论】:

  • 感谢您的回答。我已经尝试过您的解决方案,但与我看到 15 个参数(包括 JWT 令牌)的 Chrome 浏览器相比,只获得了 4 个参数。还尝试使用 Postman 请求并在响应标头中获得相同的 11 个参数。但我需要获取一个使用 https node.js lib 无法获取的适当参数(JWT 令牌)。
  • 你到底想从标题中得到什么?
  • 我想使用 https node.js lib 从响应头中获取一个必要的参数(JWT 令牌)。但是我使用 https node.js lib 在响应标头中获得了 4 个不必要的参数。当我使用 Postman 时,我会获取所有参数,包括必要的参数(JWT 令牌)。如何使用 https node.js lib 获取所有参数或仅一个需要的参数(JWT 令牌)?
  • res头中的JWT令牌如何?它应该在请求标头中
  • 是的,JWT 令牌也在前端的请求标头中。同样在后端 JWT 令牌中放入响应标头。
猜你喜欢
  • 2021-11-15
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2018-08-30
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
相关资源
最近更新 更多