jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.41k stars 125 forks source link

Method not allowed with method POST #174

Open gil2gm opened 3 years ago

gil2gm commented 3 years ago

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\"}"
}
stawecki commented 3 years ago

Hi Gilberto, The sample you've provided seems to work fine, when testing sample path /v1/bot/selector. Perhaps the issue is somewhere else.

Good luck!

slootjes commented 3 years ago

Created separate issue #179 for this as it seems to be a different issue.

gil2gm commented 3 years ago

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;
  }
jeremydaly commented 3 years ago

v0.11 supports Payload V2 and should be available shortly.

capndave commented 1 year ago

@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?

jeremydaly commented 1 year ago

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.