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.

Can't get the API Gateway event object

See original GitHub issue

I use aws-serverless-express/middleware but can’t get the API Gateway event object. Here is my code.

...
import awsServerlessExpress from 'aws-serverless-express';
import awsServerlessExpressMiddleware from 'aws-serverless-express/middleware';
...

const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
api.use(awsServerlessExpressMiddleware.eventContext());

app.use('/', api);
const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);

When i use access req.apiGateway.event, error has occurred like below. TypeError: Cannot read property 'event' of undefined

Am i missing something? or is this bug?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15

github_iconTop GitHub Comments

12reactions
cjander18commented, Apr 5, 2020

In case any other lost souls who are encountering Missing x-apigateway-event or x-apigateway-context header(s) when doing local testing (e.g. from Postman) find their way here… You have to set both of the x-apigateway-event and x-apigateway-context headers in Postman. The important thing is their values have to be

encodeURIComponent(JSON.stringify({"example": "data here" }));

So for instance, if the header key is: x-apigateway-event Then the value should be %7B%22data%22%3A%22Sand%22%2C%22public_key%22%3A%22key%22%7D

Note, there are no quotes around the encoded and stringified value!

You have to set both x-apigateway-event and x-apigateway-context headers because if you peek in your node_modules/aws-serverless-express/src/middleware.js file you’ll see it has the following lines:


  if (!req.headers['x-apigateway-event'] || !req.headers['x-apigateway-context']) {
    next()
    return
  }

  req[reqPropKey] = {
    event: JSON.parse(decodeURIComponent(req.headers['x-apigateway-event'])),
    context: JSON.parse(decodeURIComponent(req.headers['x-apigateway-context']))
  }
1reaction
adham-123commented, Oct 28, 2021

aws-serverless-express library is being deprecated and moving to vendia. Here is how it should be done using vendia library

const { getCurrentInvoke } = require('@vendia/serverless-express')

app.get('/', (req, res) => {
  const { event, context } = getCurrentInvoke()

  res.json(event)
})

Guide: https://github.com/vendia/serverless-express#accessing-the-event-and-context-objects

Source: https://www.npmjs.com/package/aws-serverless-express#:~:text=AWS Serverless Express library is moving to Vendia and will be rebranded to serverless-express. Similarly%2C the aws-serverless-express NPM package will be deprecated in favor of a new serverless-express package

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using AWS Lambda with Amazon API Gateway
This section explains general information on how to choose an API type, add an endpoint to your Lambda function, and information on events,...
Read more >
Can't access 'event' properties when calling it in Lambda ...
So I believe I solved the issue. I simply un-ticked the 'use lambda proxy integration' box when setting up the post method in...
Read more >
How to create a Request object for your Lambda event from ...
In your AWS Console open up your API Gateway and find the method you want to provide headers. Locate the Integration Request box...
Read more >
Fix the Most Common API Gateway Request Errors - Dashbird
Again, a retry doesn't help here. If you use end-user authentication with AWS Cognito, every request will get a temporary role related to...
Read more >
API (REST) - Fetching data - JavaScript - AWS Amplify Docs
Using the GET API REST in Amplify - JavaScript - AWS Amplify Docs. ... access your query parameters & body within your Lambda...
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