I’m trying to figure out how to send URL parameters to my Lambda function through API Gateway. I want to handle both query string and path parameters.
For example, I’d like to process these two URL formats:
GET /profile?username=alice
GET /profile/alice
How can I make sure the Lambda function receives the ‘username’ parameter in both cases?
I’ve been looking at the API Gateway settings, but I’m a bit lost. I remember reading something about ‘mapped from’ in the docs, but I can’t find that option in the console.
I set up a query string parameter, but I don’t see any way to map it to the Lambda function. I thought I’d see options like:
method.request.path.username
method.request.querystring.username
Am I missing something obvious here? Any help would be great!
I’ve worked with this setup before, and here’s what I found to be the most straightforward approach:
For query string parameters, they’re automatically included in the event object passed to your Lambda function. You can access them via event.queryStringParameters.
For path parameters, you need to define them in your API Gateway resource path (like /profile/{username}). Then, in your Lambda function, you can retrieve them from event.pathParameters.
The key is to enable Lambda Proxy integration in API Gateway. This setting automatically passes all request data to your Lambda function, including both query string and path parameters.
In your Lambda function, you can then handle both cases like this:
const username = event.queryStringParameters?.username || event.pathParameters?.username;
This way, you’re covered for both URL formats you mentioned. Hope this helps clarify things!
Hey there! I’ve actually gone through this exact setup recently, so I can share what worked for me.
For query params, it’s pretty straightforward. They automatically show up in your Lambda event object under event.queryStringParameters
. No special configuration needed there.
Now, for path parameters, you’ll need to set up your API Gateway resource path like /profile/{username}
. Once you do that, you can grab the username from event.pathParameters
in your Lambda function.
The key thing that made it all click for me was enabling Lambda Proxy integration in API Gateway. It’s like magic – suddenly all the request data, including both types of parameters, just flows into your Lambda function.
In your code, you can handle both cases like this:
const username = event.queryStringParameters?.username || event.pathParameters?.username;
This way, you’re covered no matter which URL format they use. It’s been working great for me, and it’s way simpler than I initially thought. Give it a shot and let me know if you run into any snags!
yo, i’ve used this setup before. query params show in event.queryStringParameters and for path params, define /{username} and grab it from event.pathParameters. tick lambda proxy integration box and it’ll work, hope it helps!