javascript 如何在 2018 年访问 AWS Lambda 中的标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48754991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:15:37  来源:igfitidea点击:

How to Access header in AWS Lambda in 2018

javascriptnode.jsamazon-web-servicesaws-lambda

提问by Michael Coxon

Yes, I know this is a duplicate, however things have changed since the Mapping Template solution provided here, hereand herewas devised. With a proxy integration (the AWS recommended approach), there is no access to templates. So how do you get to the headers now?

是的,我知道这是重复的,但是自从设计了此处此处此处提供的映射模板解决方案以来,情况发生了变化。通过代理集成(AWS 推荐的方法),无法访问模板。那么你现在如何获得标题?

I have tried just using the object model with things like:

我曾尝试将对象模型与以下内容一起使用:

event.headers
event.headers["X-Requested-With"]

var headerItem = "x-requested-with"
event.headers.headerItem

etc. and nothing seems to be defined.

等等,似乎没有任何定义。

The event, according to Cloudwatch, is:

根据 Cloudwatch 的说法,该事件是:

{
    "resource": "/contactformlambda",
    "path": "/contactformlambda",
    "httpMethod": "POST",
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "en-US,en;q=0.9",
        "cache-control": "no-cache",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "AU",
        "content-type": "text/plain",
        "Host": "ovo5xmxf7e.execute-api.ap-southeast-2.amazonaws.com",
        "origin": "http://localhost:4200",
        "pragma": "no-cache",
        "Referer": "http://localhost:4200/contact",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
        "Via": "2.0 49d473f12cd3746d92748f257e16ca9e.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "7_PvRhkwbB7wmB1n8EFgE9s84q1xPYZ_uKwAjwYPXSv383M-fmDTgQ==",
        "X-Amzn-Trace-Id": "Root=1-5a826e92-b4425f8069686c808cc2d500",
        "X-Forwarded-For": "155.144.114.41, 54.240.152.46",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https",
        "x-requested-with": "Angular5"
    },
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "requestTime": "13/Feb/2018:04:50:26 +0000",
        "path": "/prod/contactformlambda",
        "accountId": "499908792600",
        "protocol": "HTTP/1.1",
        "resourceId": "i6i1qv",
        "stage": "prod",
        "requestTimeEpoch": 1518497426058,
        "requestId": "683aeec6-1079-11e8-a419-318ae32195ef",
        "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "sourceIp": "155.144.114.41",
            "accessKey": null,
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
            "user": null
        },
        "resourcePath": "/contactformlambda",
        "httpMethod": "POST",
        "apiId": "ovo5xmxf7e"
    },
    "body": "{\"subject\":\"Enquiry from ZenithWebFoundry\",\"name\":\"Mike\",\"number\":\"0415118180\",\"email\":\"[email protected]\",\"comment\":\"this is a test from localhost\"}",
    "isBase64Encoded": false
}

The "x-requested-with" header is in there, but it never seems to be defined, when I try to access it programmatically

“x-requested-with”标头在那里,但当我尝试以编程方式访问它时,它似乎从未被定义过

回答by dashmug

It should be in event.headers.

它应该在event.headers.

From documentation, it states...

文档中,它指出......

{
    "resource": "Resource path",
    "path": "Path parameter",
    "httpMethod": "Incoming request's method name"
    "headers": {Incoming request headers}
    "queryStringParameters": {query string parameters }
    "pathParameters":  {path parameters}
    "stageVariables": {Applicable stage variables}
    "requestContext": {Request context, including authorizer-returned key-value pairs}
    "body": "A JSON string of the request payload."
    "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
}


Update (based on your new supplied information):

更新(基于您提供的新信息):

The header that you're looking for is x-requested-withNOT X-Requested-With(note the casing).

您要查找的标题x-requested-with不是X-Requested-With(注意大小写)。

// This will yield nothing, as it is the wrong key
event.headers['X-Requested-With']

// This will give you what you need.
event.headers['x-requested-with']

Take note that object lookup in Javascript is case-sensitive.

请注意,Javascript 中的对象查找区分大小写。

If you want to retrieve it using a variable, you can do the following...

如果要使用变量检索它,可以执行以下操作...

var headerItem = "x-requested-with"
event.headers[headerItem]