响应未定义-AWS API网关客户端 [英] Response undefined - aws-api-gateway-client

查看:77
本文介绍了响应未定义-AWS API网关客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个AWS API Gateway来调用Lambda函数以生成随机数:

I have created an AWS API Gateway to invoke a Lambda function to generate random numbers:

Lambda函数:

exports.handler = (event, context, callback) => {
let min = parseInt(event.min);
let max = parseInt(event.max);
let generatedNumber = Math.floor(Math.random() * max) + min;
context.done(null, {generatedNumber: generatedNumber});
};

API网关中用于获取方法的身体映射模板:

Body mapping Template in API gateway for get method:

{
    "min" : $input.params('min'),
    "max" : $input.params('max')
}

当我如下访问API端点时: https://abcdefgh. execute-api.ap-south-1.amazonaws.com/DEV/number?min=10&max=20

When I access API endpoint like below: https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number?min=10&max=20

我得到了正确的答复:

{"generatedNumber":28}

但是当我尝试使用aws-api-gateway-client访问node.js中的API时,我收到以下响应:

But when I try to access the API in node.js using aws-api-gateway-client I am receiving the below response :

_currentUrl: 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number' },
  response: undefined

当前网址应设置为" https://abcdefgh.execute-api.ap-south-1.amazonaws .com/DEV/number ".

The current url should be set to 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number?min=20&max=40' but it is set to 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV/number'.

这是我访问此api的node.js代码:

Here is my node.js code to access this api:

let AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
//AWS.config.region = 'ap-south-1';
let lambda = new AWS.Lambda();
let apigClientFactory = require('aws-api-gateway-client').default;
let config = {
    invokeUrl: 'https://abcdefgh.execute-api.ap-south-1.amazonaws.com/DEV',
    accessKey: '<access-key>',
    secretKey: '<secret-key>',
    region: 'ap-south-1'
};
let apigClient = apigClientFactory.newClient(config);
let apiParams = '{"min": 20,"max": 40}';
let body = {
    
}

let additionalParams = {
   
}
apigClient.invokeApi(apiParams, '/number', 'GET', additionalParams, body)
    .then(function (result) {
        console.log(result);
    })
    .catch(function (error) {
        console.log(error);
    });

我尝试将apiParams更改为:

I tried changing apiParams to :

let apiParams = {"min": 20,"max": 40};

我收到以下错误: '{"message": "Could not parse request body into json: Unexpected character (\\\',\\\' (code 44)): expected a value\\n at [Source: [B@42feb146; line: 2, column: 14]"}' } } 我的代码有什么问题?

The I receive the below error: '{"message": "Could not parse request body into json: Unexpected character (\\\',\\\' (code 44)): expected a value\\n at [Source: [B@42feb146; line: 2, column: 14]"}' } } What is wrong in my code?

预先感谢

推荐答案

我发现了问题.我需要在AdditionalParmae​​ters对象中传递参数,例如:

I found the problem. I need to pass parameters in additionalParmaeters object like :

let additionalParams = {
   queryParams: {
   min: 20, max: 40
   }
}

但是文字

var params = {
    //This is where any header, path, or querystring request params go. The key is the parameter named as defined in the API
    userId: '1234',
};

具有误导性,因为在将参数添加到params对象时可能不会传递查询参数(也许是对我而言),而是仅在将其传递给AdditionalPrams时传递.

is misleading because query parameters were not passed when parameters were added to params object ( maybe it was for me ), but were only passed when passed inside additionalPrams.

希望有帮助.

这篇关于响应未定义-AWS API网关客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆