如何使用 aws lambda 通过 nodejs 发布 jwt 令牌以及请求
我有以下代码,可以正确访问远程 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
Authorization
标头传递 JWT 令牌:请不要在生产代码中对令牌进行硬编码。在事件中传递它、在环境变量中设置它、从 SSM 加载它或使用 AppConfig。
You pass the JWT token using the
Authorization
header: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.