422 {"error":{"type":"bad_response_status_code","message":"Unsupported request b...

2025年12月27日 21:17 状态: processing

🚨 错误信息

API Error: 422 {"error":{"type":"bad_response_status_code","message":"Unsupported request body. Please adhere to the Claude specification. (request id: 20251227211707271000899kDTS0A2j)"},"type":"error"}

🤖 AI解决方案

代码报错分析报告

错误翻译

json
原文:Unsupported request body. Please adhere to the Claude specification.
译文:不支持的请求体。请遵守 Claude 规范。

原因分析

这是一个 API 调用错误,具体表现为 HTTP 422 状态码(Unprocessable Entity)

根本原因:向 Claude API 发送的请求体格式不符合其规范要求

具体分析

  • 错误类型bad_response_status_code 表示服务器接收到了请求,但无法处理其中的数据

  • 直接原因:请求体(request body)的结构、字段或格式与 Claude API 的要求不匹配

  • 问题位置:API 调用请求的构建部分
  • 解决方案

    Python 示例修复

    python
    import requests
    import json

    错误示例(可能导致 422 错误)


    payload = {


    "prompt": "Hello Claude", # 可能使用了错误的字段名


    "max_tokens": 100


    }

    ✅ 修复后的正确格式(根据 Claude API 规范)


    headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
    }

    payload = {
    "model": "claude-3-sonnet-20241022", # 指定模型版本
    "messages": [
    {
    "role": "user",
    "content": "Hello Claude"
    }
    ],
    "max_tokens": 100
    }

    发送请求


    response = requests.post(
    "https://api.anthropic.com/v1/messages",
    headers=headers,
    json=payload # 使用 json 参数自动序列化
    )

    print(f"状态码: {response.status_code}")
    print(f"响应: {response.json()}")

    JavaScript/Node.js 示例修复

    javascript
    const axios = require('axios');

    // ✅ 正确的请求体格式
    const payload = {
    model: 'claude-3-sonnet-20241022',
    messages: [
    {
    role: 'user',
    content: 'Hello Claude'
    }
    ],
    max_tokens: 100
    };

    const headers = {
    'Content-Type': 'application/json',
    'x-api-key': 'your-api-key',
    'anthropic-version': '2023-06-01' // 版本头很重要
    };

    axios.post('https://api.anthropic.com/v1/messages', payload, { headers })
    .then(response => console.log(response.data))
    .catch(error => console.error('错误:', error.response?.data || error.message));

    ️ 预防措施

    1. 查阅官方文档


  • 始终参考最新的官方 API 文档:[Anthropic Claude API Documentation](https://docs.anthropic.com/claude/reference/messages_post)

  • API 规范会更新,特别是模型版本和必需字段
  • 2. 使用验证工具


  • API 客户端工具:使用 Postman 或 Insomnia 预先测试请求

  • Schema 验证:使用 JSON Schema 验证请求体结构

  • 代码检查
  • python

    请求发送前验证


    import jsonschema

    定义 Claude API 的 schema(简化示例)


    claude_schema = {
    "type": "object",
    "required": ["model", "messages"],
    "properties": {
    "model": {"type": "string"},
    "messages": {"type": "array"},
    "max_tokens": {"type": "integer", "minimum": 1}
    }
    }

    验证请求体


    try:
    jsonschema.validate(payload, claude_schema)
    print("✅ 请求体格式正确")
    except jsonschema.ValidationError as e:
    print(f"❌ 验证失败: {e.message}")

    快速检查清单

  • ✅ Content-Type 是否正确设置为 application/json

  • ✅ 模型名称是否正确且为最新版本

  • ✅ messages 数组格式是否正确

  • ✅ 必需的头部字段是否完整(如 anthropic-version)

  • ✅ API 密钥是否有访问权限