How to get the HTTP method in AWS Lambda?

后端 未结 3 793
没有蜡笔的小新
没有蜡笔的小新 2021-01-04 06:26

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

I understand from the documentation tha

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 06:42

    The context.httpMethod approach works only in templates. So, if you want to have access to the HTTP method in your Lambda function, you need to find the method in the API Gateway (e.g. GET), go to the Integration Request section, click on Mapping Templates, and add a new mapping template for application/json. Then, select the application/json and select Mapping Template and in the edit box enter something like:

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

    Then, when your Lambda function is called, you should see a new attribute in the event passed in called http_method which contains the HTTP method used to invoke the function.

提交回复
热议问题