4

I want to build some routes for my todos, example:

  • List
  • Get
  • etc

So in order to do this I thought to check for the URL invoking the API (please if there is a better way call me out). I'm trying it out in a simple lambda first but can't get the URL, this is what I tried:

'use strict';

exports.handler = async (event) => {
    
    let itsCallingFrom = event.requestContext.pathParameters;
    
    const response = {
        statusCode: 200,
        body: JSON.stringify('Calling from: ' + itsCallingFrom),
    };
    return response;
};

This is how my Route looks:

/listalltodos
    GET

This is what the event shows:

enter image description here

This is what I get: "Calling from: undefined"

Any idea how to get it?

Thanks

1
  • A bit late to the party, but: The pathParameters are in the event not in the event.requestContext ... Furthermore event.pathParameters is an object so concatenating to a string might give some unexpected result (ie it's stringified as [object Object]) But as your route doesn't seem to have any parameters (for instance some element id or similar, ie a variable part in the path) event.pathParameters may also be null or undefined Commented Dec 1, 2021 at 11:28

1 Answer 1

3

The form of event object in HTTP api is shown here. It does not have a parameter such as pathParameters.

Instead you can use:

  • event.rawQueryString
  • event.rawPath

Or if you just want parameters then you can use:

  • event.queryStringParameters - this will not be present if parameters are not provided, so you can use:
let itsCallingFrom = event.queryStringParameters || 'none';
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, I was looking for event.rawPath that worked :) Thanks Marcin
Maybe the documentation changed in the meantime, but the referenced documentation clearly states, that there is indeed a event.pathParameters (although it might not always be present if the matched route doesn't define any parameters)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.