Serverless部署Lambda集成API遇Unsupported Media Type错误求助
I've run into this exact issue before when setting up Lambda-integrated APIs with Serverless Framework—here's how to resolve the "Unsupported Media Type" error and properly configure your Method Responses and Integration Responses:
Root Cause of the Error
The error typically pops up for two main reasons:
- API Gateway isn't configured to parse incoming requests with the correct media type (most commonly
application/json). - The Method/Integration Responses aren't mapped properly to translate Lambda's output into valid HTTP responses that match your defined models.
Full Working Configuration
Here's the updated serverless.yml snippet with all necessary configurations:
events: - http: path: /path method: post integration: lambda # Configure request handling to resolve Unsupported Media Type request: # Convert incoming JSON requests to a format Lambda can process contentHandling: CONVERT_TO_JSON parameters: headers: # Ensure API Gateway accepts Content-Type headers Content-Type: true # Define Method Responses (what API Gateway exposes to clients) methodResponses: - statusCode: 200 responseModels: # Map application/json responses to AWS's default Empty model, or your custom model application/json: "Empty" - statusCode: 400 responseModels: application/json: "Error" # Define Integration Responses (map Lambda's output to Method Responses) integrationResponses: - statusCode: 200 # Match Lambda responses where the statusCode field is 200 selectionPattern: "^200$" # Pass Lambda's response body directly as JSON responseTemplates: application/json: "$input.path('$')" - statusCode: 400 selectionPattern: "^400$" responseTemplates: application/json: "$input.path('$')"
Custom Response Models (Optional)
If you need custom validation or structured responses, define your own API Gateway models in the resources section of your serverless.yml:
resources: Resources: # Custom success response model SuccessResponseModel: Type: AWS::ApiGateway::Model Properties: RestApiId: Ref: ApiGatewayRestApi Name: SuccessResponse Description: Structured success response ContentType: application/json Schema: type: object properties: message: type: string data: type: object required: [message] # Custom error response model ErrorResponseModel: Type: AWS::ApiGateway::Model Properties: RestApiId: Ref: ApiGatewayRestApi Name: ErrorResponse Description: Structured error response ContentType: application/json Schema: type: object properties: error: type: string code: type: integer required: [error]
Then update your methodResponses to use these custom models:
methodResponses: - statusCode: 200 responseModels: application/json: "SuccessResponse" - statusCode: 400 responseModels: application/json: "ErrorResponse"
Key Notes to Remember
Lambda Response Format: Make sure your Lambda function returns a structured object like this:
return { statusCode: 200, body: JSON.stringify({ message: "Success!", data: {} }) };The
selectionPatterninintegrationResponsesmatches against thestatusCodevalue from Lambda's response.Content Handling: The
request.contentHandling: CONVERT_TO_JSONensures API Gateway automatically parses incomingapplication/jsonrequests into a JSON object that Lambda can consume, which fixes the "Unsupported Media Type" error.Response Templates: The
responseTemplatesentry$input.path('$')passes Lambda's full response directly to the client—adjust this if you need to transform the response format before sending it.
内容的提问来源于stack exchange,提问作者johnny_mac

