# fastapi.exceptions.WebSocketRequestValidationError: 400 错误的 WebSocket 请求

- **ID:** `python/fastapi-websocket-validation-error`
- **领域:** python
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

WebSocket 连接请求具有无效的参数或头部。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Ensure the WebSocket URL includes the correct subprotocols and headers: websocket.connect("ws://...", subprotocols=["chat"])
   ```
2. **** (85% 成功率)
   ```
   Handle the validation error in the endpoint: except WebSocketRequestValidationError as e: await websocket.close(code=1002)
   ```

## 无效尝试

- **** — This can lead to security vulnerabilities or broken connections. (70% 失败率)
- **** — This breaks the WebSocket functionality entirely. (90% 失败率)
