You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Serverless部署Lambda集成API遇Unsupported Media Type错误求助

Fixing "Unsupported Media Type" & Configuring Responses in Serverless.yml

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

  1. Lambda Response Format: Make sure your Lambda function returns a structured object like this:

    return {
      statusCode: 200,
      body: JSON.stringify({ message: "Success!", data: {} })
    };
    

    The selectionPattern in integrationResponses matches against the statusCode value from Lambda's response.

  2. Content Handling: The request.contentHandling: CONVERT_TO_JSON ensures API Gateway automatically parses incoming application/json requests into a JSON object that Lambda can consume, which fixes the "Unsupported Media Type" error.

  3. Response Templates: The responseTemplates entry $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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:14:18