简体   繁体   English

AWS 网关 Lambda Cookie

[英]AWS Gateway Lambda Cookies

I am having a problem finding a way of how to access the cookie that is sent to my AWS Lambda.我在寻找如何访问发送到我的 AWS Lambda 的 cookie 的方法时遇到问题。 I have an API Gateway with lambda proxy integration (POST) to my Lambda.我有一个带有 lambda 代理集成 (POST) 到我的 Lambda 的 API 网关。

From my client java program I am making an http POST request with body(Json) and with an HttpCookie in the CookieStore inside the CookieManager .从我的客户端 java 程序中,我正在使用 body(Json) 和CookieStore内的CookieManagerHttpCookie发出 http POST 请求。 This request goes to my Lambda where the body is printed to the console.此请求发送到我的 Lambda,其中正文被打印到控制台。 This works fine but unfortunately I don't know how to access the cookie that was sent with the POST request.这工作正常,但不幸的是我不知道如何访问随 POST 请求发送的 cookie。 I am using Java 8 and APIGatewayProxyRequestEvent to get the body.我正在使用 Java 8 和APIGatewayProxyRequestEvent来获取正文。

Can anybody please provide me some assistance on how to get the cookie from APIGatewayProxyRequestEvent event or maybe another workaround?有人可以就如何从APIGatewayProxyRequestEvent事件或其他解决方法中获取 cookie 向我提供一些帮助吗?

Thank you!谢谢!

The cookies are sent in the Headers within the APIGatewayProxyRequestEvent object — request.Headers.Cookie . cookie 是在APIGatewayProxyRequestEvent对象 - request.Headers.Cookie内的APIGatewayProxyRequestEvent Headers中发送的。

The important thing is to parse the cookies since values of Headers are always String .重要的是解析 cookie,因为Headers值总是String

Here's how you can access it and create a key-value HashMap so that it's easier to use.下面介绍了如何访问它并创建一个键值HashMap以使其更易于使用。

String cookiesStr = request.getHeaders().getOrDefault("Cookie", "");
String[] cookiesArr = cookiesStr.split(";");

Map<String, String> cookiesMap = new HashMap<>();
String[] cookieSplits;

for(String cookie : cookiesArr) {
    cookieSplits = cookie.trim().split("=");
    cookiesMap.put(cookieSplits[0], cookieSplits[1]);
}

Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
logger.log("CookiesMap:\n" + gson.toJson(cookiesMap));

The output of that will look something like this:其输出将如下所示:

CookiesMap:
{
    "TestCookie1": "value1",
    "TestCookie2": "value2"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM