如何使用 aws lambda 通过 nodejs 发布 jwt 令牌以及请求

发布于 2025-01-11 17:05:37 字数 813 浏览 0 评论 0原文

我有以下代码,可以正确访问远程 API 并按预期返回结果。

const http = require('http');
exports.handler = async (event) => {
    let dataString = '';
    
    const response = await new Promise((resolve, reject) => {
        const req = http.get("url", function(res) {
          res.on('data', chunk => {
            dataString += chunk;
          });
          res.on('end', () => {
            resolve({
                statusCode: 200,
                body: JSON.stringify(JSON.parse(dataString), null, 4)
            });
          });
        });
        
        req.on('error', (e) => {
          reject({
              statusCode: 500,
              body: 'Something went wrong!'
          });
        });
    });
    
    return response;
};

但我必须使用 jwt 令牌才能正确访问此 api。我怎样才能做同样的事情?

I have the following code which hits the remote API properly and returning result as expected.

const http = require('http');
exports.handler = async (event) => {
    let dataString = '';
    
    const response = await new Promise((resolve, reject) => {
        const req = http.get("url", function(res) {
          res.on('data', chunk => {
            dataString += chunk;
          });
          res.on('end', () => {
            resolve({
                statusCode: 200,
                body: JSON.stringify(JSON.parse(dataString), null, 4)
            });
          });
        });
        
        req.on('error', (e) => {
          reject({
              statusCode: 500,
              body: 'Something went wrong!'
          });
        });
    });
    
    return response;
};

But i have to use a jwt token to hit this api properly. How can i do the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2025-01-18 17:05:37

您可以使用 Authorization 标头传递 JWT 令牌:

const http = require('http');
exports.handler = async (event) => {
    let dataString = '';
    const token = "...." // <- your JWT token
    
    const response = await new Promise((resolve, reject) => {
        const options = {
            "headers": {"Authorization": "Bearer " + token}
        }
        const req = http.get("url", options, function(res) { [...] }
    });
    
    return response;
};

不要在生产代码中对令牌进行硬编码。在事件中传递它、在环境变量中设置它、从 SSM 加载它或使用 AppConfig。

You pass the JWT token using the Authorization header:

const http = require('http');
exports.handler = async (event) => {
    let dataString = '';
    const token = "...." // <- your JWT token
    
    const response = await new Promise((resolve, reject) => {
        const options = {
            "headers": {"Authorization": "Bearer " + token}
        }
        const req = http.get("url", options, function(res) { [...] }
    });
    
    return response;
};

Please do not hardcode the token in your production code. Either pass it in the event, set it in the environment variables, load it from SSM or use AppConfig.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文