Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

amazon web services - Get URL that invoked the AWS HTTP API using Lambda - Node.js

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

question from:https://stackoverflow.com/questions/65866667/get-url-that-invoked-the-aws-http-api-using-lambda-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...