AWS Gateway Lambda Cookies

1.3k views Asked by At

I am having a problem finding a way of how to access the cookie that is sent to my AWS Lambda. I have an API Gateway with lambda proxy integration (POST) to my Lambda.

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. This request goes to my Lambda where the body is printed to the console. This works fine but unfortunately I don't know how to access the cookie that was sent with the POST request. I am using Java 8 and APIGatewayProxyRequestEvent to get the body.

Can anybody please provide me some assistance on how to get the cookie from APIGatewayProxyRequestEvent event or maybe another workaround?

Thank you!

1

There are 1 answers

0
ᴛʜᴇᴘᴀᴛᴇʟ On

The cookies are sent in the Headers within the APIGatewayProxyRequestEvent object — request.Headers.Cookie.

The important thing is to parse the cookies since values of Headers are always String.

Here's how you can access it and create a key-value HashMap so that it's easier to use.

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"
}