python
data_error
ai_generated
true
json.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ID: python/starlette-request-body-parsing
80%Fix Rate
83%Confidence
0Evidence
2024-04-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Request body is empty or not valid JSON when trying to parse with request.json().
generic中文
请求体为空或不是有效的 JSON,但尝试用 request.json() 解析。
Workarounds
-
90% success
Check content-type first: if request.headers.get('content-type') == 'application/json': data = await request.json() else: return JSONResponse(status_code=415) -
85% success
Use a middleware to parse JSON and handle errors gracefully.
Dead Ends
Common approaches that don't work:
-
50% fail
Works but masks the root cause; better to validate request content type.
-
60% fail
body() returns bytes; need to decode and json.loads.