访问HTTP请求(头,查询字符串,饼干,体)的Lambda对象,HTTP终结 [英] Access HTTP request (headers, query string, cookies, body) object in lambda with http endpoint
问题描述
我想看看如何从与访问请求,标题和正文中的值的lambda code。如果请求的身体是JSON格式,它会自动似乎被解析并在事件对象提供。
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 made available in the event object.
我如何可以访问完整的查询字符串,要求身体,请求头(饼干)的任何类型的来袭的Content-Type的要求内LAMBDA?
How can I access the complete query string, request body, request headers (cookies) for any type of incoming "Content-Type" request inside Lambda ?
的编辑下面是我收集来帮助解决,可能会或可能不相关的问题的信息。请忽略他们,如果你想。的
编辑:
我通过SE <一个中存在的问题去href="http://stackoverflow.com/questions/31372167/how-to-access-http-headers-for-request-to-aws-api-gateway-using-lambda?rq=1">here和<一href="http://stackoverflow.com/questions/31329958/how-to-pass-a-querystring-or-route-parameter-to-aws-lambda-from-amazon-api-gatew?rq=1">here.
按本线程,使用 $ input.json('$')
应该做的伎俩。我想从这些环节上面已经默认过时的API网关的答案似乎认识到JSON的请求,并因此使其可在事件
对象没有任何正在配置映射模板。
I went through the existing questions on SE here and here.
As per this thread, using $input.json('$')
should do the trick. I guess the answers from these links above are already out-dated as API gateway by default seems to recognize JSON in the request and if so makes it available in the event
object without any mapping templates being configured.
设置映射的建议对我不起作用。它不包含请求标题信息。
Setting the mapping as suggested does not work for me. It does not contain the request header information.
下面是它是如何配置的屏幕截图。
Here are screen shots on how it is configured.
在标题键返回空值。使用 $ input.params('$')
或$ input.params($),
错误出
The "headers" key returns a blank value. Using $input.params('$')
or "$input.params('$')"
errors out.
编辑2
试图界定方法请求头。还是没有得到内部拉姆达的用户代理值。
Tried defining the headers in Method Request. Still not getting the User-Agent value inside lambda.
编辑3
我用下面的模板映射在API网关
I used the following template mapping at the API Gateway
{
"request": $input.json('$'),
"headers": "$input.params()"
}
和下面的code在lambda
and the below code in lambda
context.succeed("event.key32:"+JSON.stringify(event, null, 2) );
And the response generated by the API gateway shows this
看着在响应中的头的价值,它看起来像AWS-SDK / API网关/ CloudFront的剥掉从HTTP客户端接收的所有头?下面是由input.params $()返回的JSON全文。标题
Looking at the "headers" value in the response, it looks like the AWS-SDK/API gateway/cloudfront strips off all headers received from the HTTP client ? Here is the full text from the JSON returned by the $input.params().header
header={CloudFront-Forwarded-Proto=https, CloudFront-Is-Desktop-Viewer=true, CloudFront-Is-Mobile-Viewer=false, CloudFront-Is-SmartTV-Viewer=false, CloudFront-Is-Tablet-Viewer=false, Content-Type=application/json, Via=1.1 5d53b9570d94ce920abbd471.cloudfront.net (CloudFront), 1.1 95eea7baa7ec95c9a41eca9e3ab7.cloudfront.net (CloudFront), X-Amz-Cf-Id=GBqmObLRy6Iem9bJbVPrrW1K3YoWRDyAaMpv-UkshfCsHAA==, X-Forwarded-For=172.35.96.199, 51.139.183.101, X-Forwarded-Port=443, X-Forwarded-Proto=https}}
有不具有用户代理字符串在报头,尽管如上述的屏幕截图示出了,它被送到由REST客户端。 有趣的是,整个查询字符串变为可用。不知道这是一个预期的方式来访问它。
It doesn't have the User-Agent string in the header, although as the screenshot shows above, it was sent by the REST client. Interestingly, the entire query string is made available. Not sure if this is an intended way to access it.
推荐答案
该请求头可以使用访问 $ input.params('头名')
The request headers can be accessed using $input.params('header-name')
出人意料的是,用户代理头不能与上述code访问。你需要通过跳跃<一个href="http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html"相对=nofollow>下面箍检索它的:
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
使用以下code的请求主体/负载应该可以访问。更多参考<一href="http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#input-variable-reference"相对=nofollow>此处,这里和这里:
The request body/payload should be accessible using the following code. More reference here, here and here:
{
"reqbody": "$input.path('$')"
}
目前尚不清楚,如果请求主体预计将在JSON。它需要指出的是,请求被视为根据此
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.
目前有似乎是两个错误的:
- 在用户代理标头缺少/由亚马逊API被剥离。
-
在头值中包含双引号(),不执行拉姆达功能。(我没有看到这样的请求CloudWatch的日志,日志条目)。相反, ,HTTP响应体包含以下内容:
- The "User-Agent" header is missing/being stripped off by the Amazon API.
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
我相信这将需要加以纠正,以便能够实现所述ETag的机制来用于高速缓存
I believe this would need to be corrected to be able to implement the ETag mechanism for caching.
参考文献:
一个ETag预计将双引号。浏览器有望通过。如果-None-Match头发送此精确值回来了,而这正是亚马逊的API休息。
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.
HTTP:ETAG 的最大长度
http://gsnedders.com/http-entity-tags-confusion
这篇关于访问HTTP请求(头,查询字符串,饼干,体)的Lambda对象,HTTP终结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!