lambda-api
lambda-api copied to clipboard
Method not allowed with method POST
Hello, i need your help!
I have problem with the api, i use register like this
//------------------------------------------------------- index.js
const api = require('lambda-api')({ version: 'v1.0', base: 'v1' });
api.register(require('./routes'), { prefix: '/bot' });
//-------------------------------------------------------
i not have problem with the method get, my problem are the methods POST, i proof the two forms, but any working
//------------------------------------------------------- routes.js
module.exports = (api, opts) => {
api.get('/list', bot.list);
api.post('/selector', bot.selector);
api.METHOD('post','/validator_response', bot.validator_response);
api.post('/hermes', bot.hermes);
};
//-------------------------------------------------------
Always i have the same response:
Response
{
"statusCode": 200,
"headers": {
"content-type": "application/json"
},
"body": "{\"error\":\"Method not allowed\"}"
}
Hi Gilberto,
The sample you've provided seems to work fine, when testing sample path /v1/bot/selector.
Perhaps the issue is somewhere else.
- Are you sure you're passing the bot.selector/bot.hermes handlers correctly?
- Are you sure you're testing the correct path?
- What are you using for invoking test events?
- Have you also tried printing all the routes with:
api.routes(true);
Good luck!
Created separate issue #179 for this as it seems to be a different issue.
I found the problem, lambda-api not working with version 2.0 of api-gateway
request.js
// Set the method
this.method = this.app._event.httpMethod ? this.app._event.httpMethod.toUpperCase() : 'GET'
api-gateway aws v1.0
{
version: '1.0',
httpMethod: 'POST',
api-gateway aws v2.0
{
"version": "2.0",
"requestContext": {
"http": {
"method": "POST",
Solution 1: configure api gateway aws with the version 1.0 or Solution 2: temporal, set in handler function
if(event.version === "2.0"){
event.httpMethod = event.requestContext.http.method;
}
v0.11 supports Payload V2 and should be available shortly.
@jeremydaly we're unable to POST (and perhaps to put?) when running locally using Node 16, but Node 14 works fine. Is that consistent with the Payload V2 thing? Could our failure be related to the type of payload?
Strange the Node version would be causing a problem unless it has something to do with the body parser. Shouldn't have anything to do with the payload format.