Discussed in #9305
Originally posted by harpaj March 23, 2023
First Check
Commit to Help
Example Code
from typing import Annotated
from fastapi import FastAPI, Form
from pydantic import Json, BaseModel
app = FastAPI()
class JsonListModel(BaseModel):
json_list: Json[list[str]]
@app.post("/working")
async def working(json_list: Annotated[str, Form()]) -> list[str]:
model = JsonListModel(json_list=json_list)
return model.json_list
@app.post("/broken")
async def broken(json_list: Annotated[Json[list[str]], Form()] ) -> list[str]:
return json_list
Description
In the example code above, I would expect working and broken to be approximately equivalent.
However, while working returns the parsed json_list as expected, broken fails with
{"detail":[{"loc":["body","json_list"],"msg":"JSON object must be str, bytes or bytearray","type":"type_error.json"}]}
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.95.0
Python Version
Python 3.10.8
Additional Context
sample request that fails with broken and works with working:
import requests
import json
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(
"http://0.0.0.0:8000/broken",
data={"json_list": json.dumps(["abc", "def"])},
headers=headers,
)
```</div>
Discussed in #9305
Originally posted by harpaj March 23, 2023
First Check
Commit to Help
Example Code
Description
In the example code above, I would expect
workingandbrokento be approximately equivalent.However, while
workingreturns the parsed json_list as expected,brokenfails with{"detail":[{"loc":["body","json_list"],"msg":"JSON object must be str, bytes or bytearray","type":"type_error.json"}]}Operating System
Linux
Operating System Details
No response
FastAPI Version
0.95.0
Python Version
Python 3.10.8
Additional Context
sample request that fails with
brokenand works withworking: