lambda-api icon indicating copy to clipboard operation
lambda-api copied to clipboard

Method not allowed with method POST

Open gil2gm opened this issue 4 years ago • 6 comments

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\"}"
}

gil2gm avatar Feb 27 '21 04:02 gil2gm

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!

stawecki avatar Mar 01 '21 21:03 stawecki

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

slootjes avatar Mar 04 '21 15:03 slootjes

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;
  }

gil2gm avatar Mar 27 '21 02:03 gil2gm

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

jeremydaly avatar Mar 27 '21 12:03 jeremydaly

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

capndave avatar Oct 07 '22 12:10 capndave

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.

jeremydaly avatar Oct 08 '22 12:10 jeremydaly