简体   繁体   English

AWS Lambda Chalice 应用程序未收到请求负载

[英]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.

相关问题 对 AWS lambda 函数的 curl 请求没有收到 json - curl request to AWS lambda function receives no json 如何使用 Lambda 访问 AWS API Gateway 请求的 HTTP 标头? - How to access HTTP headers for request to AWS API Gateway using Lambda? 如何在 AWS Lambda 中使用 Nodejs“请求”工作? - How to use Nodejs "request" work aysn in AWS Lambda? AWS Lambda 上的 .NET Core Web Api 在第一次请求时速度很慢 - .NET Core Web Api on AWS Lambda is slow upon first request 从 AWS Lambda 将数据发布到 Google Sheet web 应用程序 - POST data to Google Sheet web app from AWS Lambda AWS Lambda Java + ALB 缺少 HTTP 请求数据(即路径等) - AWS Lambda Java + ALB missing HTTP request data (ie path, etc) 如何在使用来自 AWS Lambda 的 307 重定向(POST 方法和正文)在 python 中传递请求正文 - How to do pass a Request Body while doing a 307 Redirect with a( POST Method and body ) from AWS Lambda , in python 从在本地系统而非 S3 上运行的 node.js 应用程序调用 AWS Lambda - Invoke AWS Lambda from a node.js app running on local system not S3 AWS lambda ResourceConflictException 部署 - AWS lambda ResourceConflictException on deployment Freshdesk 与 AWS lambda 集成 - Freshdesk integration with AWS lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM