Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method execution tests from AWS API Gateway console fail with internal error #417

Open
alexmcmanus opened this issue May 25, 2021 · 5 comments

Comments

@alexmcmanus
Copy link

The AWS API Gateway console allows you to navigate to your resource, choose "Test" and send a test request to your Lambda. When I tried this, the Lambda was throwing an error:

TypeError: Cannot read property 'if-modified-since' of null

Yet the API worked fine when accessed from a browser.

Turning up the logging, I saw this in the logs:

DEBUG	{
  message: 'SERVERLESS_EXPRESS:PROXY',
  event: '{\n' +
    "  resource: '/api/{resource+}',\n" +
    "  path: '/api/test',\n" +
    "  httpMethod: 'GET',\n" +
    '  headers: null,\n' +

Yes, it looks like headers is null.

There are two workarounds:

  • In the method execution form, define any random header in the Headers field.
  • Wrap the serverlessExpress function to provide a default value for headers:
exports.handler = function handler(event, context, callback) {
  return serverlessExpress({
    app
  })(
    {
      ...event,
      headers: event.headers || []
    },
    context,
    callback
  )
}

I know this is not a real-world issue, but I still wasted a fair amount of time trying to get to the bottom of it! I'm pretty sure I'm not doing anything odd in my code or deployment.

index.js

import express from 'express'

const app = express()
const router = express.Router()

router.get('/api/test', (req, res) => {
  res.json({ message: 'Hello World!' })
})
app.use('/', router)

export default app

CDK

    const backend = new Function(this, 'backend', {
      functionName: 'test',
      runtime: Runtime.NODEJS_14_X,
      environment: {
        AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1'
      },
      handler: 'main.handler',
      code: Code.fromAsset(path.join(path.dirname(require.resolve('test')), 'build'))
    })

    api.root.addResource('api').addResource('{resource+}').addMethod('ANY', new LambdaIntegration(backend))

package.json

  "dependencies": {
    "@vendia/serverless-express": "^4.3.7",
    "express": "^4.17.1"
  }
@derricksimpson
Copy link

When using the test capability within API Gateway, only headers added to the Headers input box (4th option from the top) are sent along. If you add anything there, such as "header: 1", this issue will go away.

@tb102122
Copy link

@derricksimpson I run into exactly the same issue. It would be helpful to ad that to the documentation somewhere.

@codpot
Copy link
Contributor

codpot commented Sep 23, 2021

I was having the same issue.
I didn't realized that this issues opened, so i opened another duplicate issue. (#462)
And also created PR for this issue. (#463)

@derricksimpson Thanks for nice temporary solution! It was helpful.

@derricksimpson
Copy link

@codpot - No problem!

@shellscape
Copy link

@derricksimpson we can probably close this one yeah?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants