How to get the HTTP method in AWS Lambda?

In AWMS Lambda code, how can I get an HTTP method (e.g. GET, POST ...) of an HTTP request coming from the AWS Gateway API?

I understand from the documentation that context.httpMethod is a solution for this.

However, I cannot get it to work.

For example, when I try to add the following 3 lines:

    if (context.httpMethod) {
            console.log('HTTP method:', context.httpMethod)
    }

in the microservice-http-endpoint schema sample AWS as follows:

exports.handler = function(event, context) {

    if (context.httpMethod) {
        console.log('HTTP method:', context.httpMethod)
    }

    console.log('Received event:', JSON.stringify(event, null, 2));

    // For clarity, I have removed the remaining part of the sample
    // provided by AWS, which works well, for instance when triggered 
    // with Postman through the API Gateway as an intermediary.
};

I never had anything in the log because httpMethod is always empty .

+4
source share
1 answer

context.httpMethod . , HTTP Lambda, Gateway API (, GET), " ", " " application/json. application/json " ", - :

{ "http_method": "$context.httpMethod" }

, Lambda, event, http_method, HTTP, .

+10

Source: https://habr.com/ru/post/1627650/


All Articles