[英]AWS Lambda Chalice app not receiving request payload
I have a small lambda function that I am creating with AWS Chalice in Python.我在 Python 中使用 AWS Chalice 创建了一个小的 lambda 函数。 When testing locally all works as intended and my test request works perfectly.
在本地测试时,所有工作都按预期进行,我的测试请求完美无缺。 However when running
chalice deploy
and then testing the same request to the hosted API I get the following error.但是,当运行
chalice deploy
然后测试对托管 API 的相同请求时,我收到以下错误。
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 1913, in _get_view_function_response
File "/var/task/app.py", line 34, in questions
payload = parse_qs(body)['data'][0]
KeyError: 'data'
app.py is as follows app.py如下
# other imports
from urllib import parse_qs
def encode_payload(payload):
'''encodes generic payload'''
return base64.b64encode(pickle.dumps(payload)).decode('utf-8')
def decode_payload(payload):
'''decodes generic payload'''
return pickle.loads(base64.b64decode(payload))
app = Chalice(app_name='markthis-api')
@app.route('/questions', methods=['GET'], content_types=['application/json'])
def questions():
style = app.current_request.query_params['style']
body = app.current_request.raw_body.decode()
payload = parse_qs(body)['data'][0]
img = decode_payload(payload)
stuff = find_questions(img, style)
return {'pickle': encode_payload(stuff)}
test.py测试.py
def api_request(img, style):
url = f'https://api-code-etc.execute-api.eu-west-2.amazonaws.com/api/questions?style={style}'
#url = f'http://localhost:8000/questions?style={style}'
headers = {'Content-Type': 'application/json'}
payload = {'data': encode_payload(img)}
return requests.get(url, data=payload, headers=headers)
I am new to AWS and Chalice and have no idea what is causing this issue.我是 AWS 和 Chalice 的新手,不知道是什么导致了这个问题。 The API has one other endoint '/' which returns
{'hello': 'world'}
. API 有一个返回
{'hello': 'world'}
另一个端点 '/'。 This works fine when deployed.这在部署时工作正常。
Any help much appreciated非常感谢任何帮助
When Content-Type is "application-json" the preferred method is using .json_body
.当 Content-Type 是“application-json”时,首选方法是使用
.json_body
。 Instead of;代替;
body = app.current_request.raw_body.decode()
Doing this might resolve the problem:这样做可能会解决问题:
body = app.current_request.json_body
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.