无服务器调用有效但 curl 出错
Serverless invoke works but curl gives error
我有一个无服务器函数,你可以在下面找到代码,这个函数是使用无服务器框架部署在aws lambda上的。在这个函数中,我调用了一个外部网络 api。
当我执行 serverless invoke -f my_func
时,我得到了预期的响应。但是如果我 运行 curl 命令失败,我得到 {"message": "Internal server error"}
这是我的 curl 命令:
curl -X GET \
https://0wb3echzu8.execute-api.us-east-1.amazonaws.com/dev/my_func \
-H 'cache-control: no-cache' \
-H 'postman-token: 1fc551c0-7010-e1e3-7287-6a0d82d1e91a'
这是我的代码:
var request = require("request");
var options = { method: 'GET',
url: 'https://api.coinmarketcap.com/v2/listings/',
headers:
{ 'postman-token': '090e284e-62ad-29f0-0f10-92ae14656a37',
'cache-control': 'no-cache' } };
module.exports.my_func = (event, context, callback) => {
request(options, function (error, response, body) {
if (error) { console.log(error); callback(null, error) }
console.log(body)
callback(null, body)
});
};
这是 serverless.yml 文件:
service: aws-nodejs
app: sonarsic
tenant: vincent
provider:
name: aws
runtime: nodejs6.10
functions:
my_func:
handler: handler.my_func
events:
- http:
path: my_func
method: get
cors: true
肯定是和我的函数调用webapi有关。如果我不调用网络 api 它工作正常。
如果我通过 serverless logs -f my_func
检查日志,我只会得到使用无服务器调用的调用日志。
在发出 curl 命令时,我如何才能找出我的函数内部出了什么问题?
将 cors:true
添加到 http 部分并不能解决问题
cloudwatch 已附加到 aws lambda,但似乎没有写入任何内容:
经过对 chat 的一些讨论,我们发现响应正文中缺少 statusCode
let request = require('request');
let options = {
method: 'GET',
url: 'https://api.coinmarketcap.com/v2/listings/',
headers: {
'postman-token': '090e284e-62ad-29f0-0f10-92ae14656a37',
'cache-control': 'no-cache',
},
};
module.exports.my_func = (event, context, callback) => {
request(options, (error, response, body) => {
if (error) { console.log(error); callback(null, error) }
console.log(body)
callback(null, { body, statusCode: 200 }) // response statusCode added
});
};
我有一个无服务器函数,你可以在下面找到代码,这个函数是使用无服务器框架部署在aws lambda上的。在这个函数中,我调用了一个外部网络 api。
当我执行 serverless invoke -f my_func
时,我得到了预期的响应。但是如果我 运行 curl 命令失败,我得到 {"message": "Internal server error"}
这是我的 curl 命令:
curl -X GET \
https://0wb3echzu8.execute-api.us-east-1.amazonaws.com/dev/my_func \
-H 'cache-control: no-cache' \
-H 'postman-token: 1fc551c0-7010-e1e3-7287-6a0d82d1e91a'
这是我的代码:
var request = require("request");
var options = { method: 'GET',
url: 'https://api.coinmarketcap.com/v2/listings/',
headers:
{ 'postman-token': '090e284e-62ad-29f0-0f10-92ae14656a37',
'cache-control': 'no-cache' } };
module.exports.my_func = (event, context, callback) => {
request(options, function (error, response, body) {
if (error) { console.log(error); callback(null, error) }
console.log(body)
callback(null, body)
});
};
这是 serverless.yml 文件:
service: aws-nodejs
app: sonarsic
tenant: vincent
provider:
name: aws
runtime: nodejs6.10
functions:
my_func:
handler: handler.my_func
events:
- http:
path: my_func
method: get
cors: true
肯定是和我的函数调用webapi有关。如果我不调用网络 api 它工作正常。
如果我通过 serverless logs -f my_func
检查日志,我只会得到使用无服务器调用的调用日志。
在发出 curl 命令时,我如何才能找出我的函数内部出了什么问题?
将 cors:true
添加到 http 部分并不能解决问题
cloudwatch 已附加到 aws lambda,但似乎没有写入任何内容:
经过对 chat 的一些讨论,我们发现响应正文中缺少 statusCode
let request = require('request');
let options = {
method: 'GET',
url: 'https://api.coinmarketcap.com/v2/listings/',
headers: {
'postman-token': '090e284e-62ad-29f0-0f10-92ae14656a37',
'cache-control': 'no-cache',
},
};
module.exports.my_func = (event, context, callback) => {
request(options, (error, response, body) => {
if (error) { console.log(error); callback(null, error) }
console.log(body)
callback(null, { body, statusCode: 200 }) // response statusCode added
});
};