Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23101

How to post JSON data that include unicode characters to FastAPI using Python requests?

$
0
0

When a FastAPI endpoint expects a Pydantic model and one is passed with a string it works as expected unless that string contains unicode characters.

First I create an example application for FastAPI with an example model.
serv.py

from pydantic import BaseModelclass exClass(BaseModel):    id: int = Field(example=1)    text: str = Field(example="Text example")app = FastAPI(debug=True)@app.post("/example")async def receive_pyd(ex: exClass):    print(ex)    return Trueif __name__ == "__main__":    import uvicorn    uvicorn.run(app, host="127.0.0.1", port=8000)

The client that shows the error in question client.py

from pydantic import BaseModel, Fieldimport requestsclass exClass(BaseModel):    id: int = Field(example=1)    text: str = Field(example="Text example")ex1 = exClass(id=1, text="working example")ex2 = exClass(id=2, text="this’ will fail")ex3 = exClass(id=3, text="🤗<- also non-working")r = requests.post(f"http://127.0.0.1:8000/example", data=ex1.model_dump_json())print(r.text)r = requests.post(f"http://127.0.0.1:8000/example", data=ex2.model_dump_json())print(r.text)r = requests.post(f"http://127.0.0.1:8000/example", data=ex3.model_dump_json())print(r.text)

Output:

trueInvalid HTTP request received.Invalid HTTP request received.

When text contains unicode characters the result is a 422 Unprocessable Entity. I have tried ex.dict(), model_dump(), and using json instead of data in the requests call. Enabling debugging in FastAPI/starlette bubbles up that the Invalid HTTP request is a JSON decode error.


Viewing all articles
Browse latest Browse all 23101

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>