我希望将cookie数据传递到AWS Lambda函数以及从AWS Lambda函数传递cookie数据,而客户端是在浏览器下运行的jQuery AJAX。对于GET方法和POST方法,我都需要这样做。
这是我的代码(不起作用)。 lambda函数在Python 3.6中
用于incrementCookie
函数的Lambda函数
from __future__ import print_function
from urllib.request import Request, urlopen
from urllib.parse import unquote
import boto3
import json
import sys
def jpath( obj, path):
res = obj
for particle in path.split('.'):
if res is None:
break
if particle in res:
res = res[particle]
else:
res = None
if res is None:
res = ''
return res
def readCookie( event, name):
value = ''
for cookie in jpath( event, 'headers.Cookie').split(';'):
pair = cookie.strip().split('=',1)
if pair[0].strip() != name:
continue
if len(pair) >= 2:
value = pair[1].strip()
else:
value = ''
break
return value
def tryInt( value, default):
if (value is None) or (value == ''):
return default
else:
try:
return int(value)
except ValueError:
return default
def buildResponse( statusCode, bumpLump):
return {
'statusCode': statusCode,
'headers': {
'Content-Type': 'application/xml',
'Access-Control-Allow-Origin': 'https://<redacted>.cloudfront.net',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': 'POST,GET,OPTIONS',
'Set-Cookie': 'bumpLump=' + str( bumpLump) + '; Secure; Path=/'
},
'body': '<bumpLump>' + str( bumpLump) + '</bumpLump>'
}
def lambda_handler(event, context):
print(event)
try:
bumpLump = tryInt( readCookie( event, 'bumpLump'), 0) + 1
except:
bumpLump = 0
response = buildResponse( 200, bumpLump)
print(response)
return response
作为swagger + APIextension导出的API是...
---
swagger: "2.0"
info:
version: "2018-09-17T12:49:17Z"
title: "incrementCookie-API"
host: "<redacted>.execute-api.ap-southeast-2.amazonaws.com"
basePath: "/demo-for-stackoverflow"
schemes:
- "https"
paths:
/incrementCookie:
options:
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Expose-Headers:
type: "string"
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Credentials:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
x-amazon-apigateway-any-method:
responses:
200:
description: "200 response"
headers:
Set-Cookie:
type: "string"
x-amazon-apigateway-integration:
uri: "arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:<redacted>:function:incrementCookie/invocations"
responses:
.*:
statusCode: "200"
passthroughBehavior: "when_no_match"
httpMethod: "POST"
type: "aws_proxy"
definitions:
Empty:
type: "object"
title: "Empty Schema"
在客户端执行的JavaScript是...
$.ajax('https://<redacted>.execute-api.ap-southeast-2.amazonaws.com/demo-for-stackoverflow/incrementCookie',{
method: 'GET',
cache: false,
dataType: 'xml',
crossDomain: true,
xhrFields: {
withCredentials: true
},
error: function( jqXHR, textStatus, errorThrown){
console.log( errorThrown);
},
success: function( response) {
console.log( response);
}
})
监控流量,浏览器发送请求...
GET https://<redacted>.execute-api.ap-southeast-2.amazonaws.com/demo-for-stackoverflow/incrementCookie?_=1537182487453 HTTP/1.1
Host: <redacted>.execute-api.ap-southeast-2.amazonaws.com
Connection: keep-alive
Accept: application/xml, text/xml, */*; q=0.01
Origin: https://<redacted>.cloudfront.net
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
DNT: 1
Referer: https://<redacted>.cloudfront.net/marketplace.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
...然后网关/ lambda响应...
HTTP/1.1 200 OK
Date: Mon, 17 Sep 2018 12:38:04 GMT
Content-Type: application/xml
Content-Length: 22
Connection: keep-alive
x-amzn-RequestId: 85a0b8e9-ba76-11e8-b57e-79f6158ed33a
Access-Control-Allow-Origin: https://<redacted>.cloudfront.net
Set-Cookie: bumpLump=1; Secure; Path=/
x-amz-apigw-id: NXX29G80SwMF2nQ=
Access-Control-Allow-Methods: POST,GET,OPTIONS
X-Amzn-Trace-Id: Root=1-5b9fa02c-e6458627603d8fff6cea9b65;Sampled=0
Access-Control-Allow-Credentials: true
<bumpLump>1</bumpLump>
但是没有设置cookie。我可以重复此呼叫,并且号码永远不会增加到2。我在做什么错了?