question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
derricksimpsoncommented, Sep 17, 2021

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.

0reactions
shellscapecommented, Jul 26, 2022

@derricksimpson we can probably close this one yeah?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve API Gateway Lambda stage variable 500 errors
When I invoke the API method, my API returns an "Internal server error" and a 500 status code. How do I resolve the...
Read more >
Resolve "Execution failed due to configuration" errors from ...
A. If your method was created by defining the AWS::ApiGateway::Method resource in your CloudFormation template, update the HttpMethod property ...
Read more >
Troubleshooting issues with HTTP API Lambda integrations
The following provides troubleshooting advice for errors and issues that you might encounter when using AWS Lambda integrations with HTTP APIs.
Read more >
Error Handling Patterns in Amazon API Gateway and AWS ...
These types of errors include internal server errors, Lambda function or account throttling, or failure of Lambda to parse the request body.
Read more >
Use the API Gateway console to test a REST API method
In the Method Execution pane, in the Client box, choose TEST. Type values in any of the displayed boxes (such as Query Strings,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found