Access HTTP request (headers, query string, cookies, body) object in lambda with http endpoint

后端 未结 4 1399
忘掉有多难
忘掉有多难 2021-02-13 23:00

I am trying to see how to access the request header and body values from with in the lambda code. If the request body is in JSON format, it automatically seems to be parsed and

4条回答
  •  醉酒成梦
    2021-02-13 23:41

    The request headers can be accessed using $input.params('header-name')

    Surprisingly, the User-Agent header cannot be accessed with above code. You need to jump through the following hoop to retrieve it:

    $context.identity.userAgent

    The request body/payload should be accessible using the following code. More reference here, here and here:

    {
       "reqbody": "$input.path('$')"
    }
    

    It is not yet clear if the request body is expected to be in JSON. It needs to be noted that the request is treated as UTF-8 according to this post.


    There currently seems to be two bugs:

    1. The "User-Agent" header is missing/being stripped off by the Amazon API.
    2. When the header values contain a double quote ("), the lambda function is not executed. (I do not see a log entry in the cloudwatch logs for such requests). Instead, the http response body contains the following:

      {
         "Type": "User",
         "message": "Could not parse request body into json."
      }
      

    An example request that fails in Amazon API

    I believe this would need to be corrected to be able to implement the ETag mechanism for caching.

    References:

    An Etag is expected to be enclosed within double quotes. The browser is expected to send this exact value back through the If-None-Match header, and this is where Amazon API breaks.

    Syntax for ETag?

    HTTP: max length of etag

    http://gsnedders.com/http-entity-tags-confusion

提交回复
热议问题