使用Flask+Zappa部署AWS Lambda时iOS端POST请求出现400错误
Hey there, let's walk through the most likely causes for that 400 Bad Request error you're seeing when hitting your /prod/chats/store endpoint from iOS, and how to fix them.
1. Mismatched Request Content-Type
A super common culprit here is that your iOS app might be sending JSON data, but your Flask code is trying to parse form data via request.form.
request.form only works with requests that have a Content-Type header set to application/x-www-form-urlencoded or multipart/form-data. If iOS is sending JSON (with Content-Type: application/json), Flask won't populate request.form at all—leading to missing fields and a 400.
Fix options:
- Update your Flask code to handle JSON:
@app.route('/chats/store/', methods=['POST']) def store_chats(): # Parse JSON instead of form data payload = request.get_json() # Check if all required keys exist in the JSON payload if payload and all(key in payload for key in ['username', 'password', 'chats']): username = payload['username'] password = payload['password'] chats = payload['chats'] # Rest of your db logic here return "Success", 200 else: return "Missing required fields", 400 - Adjust your iOS app to send form data: Ensure the request uses the correct
Content-Typeand encodes the data as form fields instead of JSON.
2. Improper Field Existence Check
Your current code uses request.form['username'] is not None to check for fields—but if a field is missing from the request, request.form['username'] will throw a KeyError instead of returning None. Flask catches this unhandled error and returns a 400 Bad Request.
Fix:
Use request.form.get() (which returns None if the key doesn't exist) or check if the key is present in request.form:
@app.route('/chats/store/', methods=['POST']) def store_chats(): # Safely retrieve fields (returns None if missing) username = request.form.get('username') password = request.form.get('password') chats = request.form.get('chats') # Verify all fields have values if username and password and chats: # Rest of your logic return "Success", 200 else: return "Missing required fields", 400
3. Zappa/Lambda Proxy Integration Issues
Occasionally, Zappa's default API Gateway proxy setup can cause request parsing weirdness. Double-check:
- Your
zappa_settings.jsonhasaws_apigateway_enabledset totrue(default for Zappa). - The API Gateway integration for your endpoint is set to Use Lambda Proxy integration (Zappa should configure this automatically, but it's worth verifying in the AWS Console).
4. Debug with Logs
To get to the root of the problem quickly, add logging to your function to see exactly what's coming in from iOS:
import logging # Set up logging for CloudWatch logger = logging.getLogger() logger.setLevel(logging.INFO) @app.route('/chats/store/', methods=['POST']) def store_chats(): # Log request headers and raw data logger.info(f"Received Request Headers: {dict(request.headers)}") logger.info(f"Raw Request Data: {request.data.decode('utf-8')}") # Your existing logic here
Then check the CloudWatch Logs for your Lambda function—this will show you the exact Content-Type and payload being sent, so you can adjust your code or iOS request accordingly.
内容的提问来源于stack exchange,提问作者Shiv

