I want to pass raw text to request body. But it has "422 Error: Unprocessable Entity" due to the \n
from posixpath import join
from fastapi import FastAPI
from pydantic import BaseModel
class Text2kgParams(BaseModel):
host: str
dataset: str
text: str
app = FastAPI()
@app.post("/text2kg")
async def contract(params: Text2kgParams):
endpoint = join(params.host, params.dataset)
return {"endpoint": endpoint, "text": params.text}
If there is text contains \n, it will throw the 422 error.


In JSON a literal line break is not allowed inside a string, it needs to be replaced by \n
{
"host": "www.demo.com",
"dataset": "ds",
"text": "This is the first paragph.\nthis is the second"
}
print(params.text)
This is the first paragph.
this is the second
@ArcLightSlavik
Thanks for the comment. But how to replace the line break with '\n' in the fastapi setting?
@BrambleXu Not sure what you mean. Fastapi settings won't deal with this, it's a json issue.
@ArcLightSlavik
Thanks. It seems I have to solve it in the front end.
Same issue I am facing with the double quotes in the request body.
@ryuzakace please share what's inside your request body. As an addition it's not an issue, it is specified in the JSON specification, and all the JSON parser implementations expects that \n.
@ryuzakace please share what's inside your request body. As an addition it's not an issue, it is specified in the JSON specification, and all the JSON parser implementations expects that
\n.
Hi ycd, in my use case, the raw text may contain double quotes. In those cases, JSON parser returns 422.
Sample request body -
{
"text" : "This is a "sample" text"
}
Delimiting the double quotes in the raw-text I cannot control.
Of course, it would fail, you can not use double quotes with the string that you created with double-quotes.
See, even syntax highlighting understands there is an error.
{
"text" : "This is a "sample" text"
}
You can use an escape character \
{
"text" : "This is a \"sample\" text"
}
Or you can use single quotes instead of double quotes
{
"text" : "This is a 'sample' text"
}
Yes.. I know. Actually, I cannot control the input. And the user can put double quotes while making request to the API.
Anyways, thanks @ycd , I believe this wasn't the right forum to post this issue as it is not actually related to the product.
Hmm, well you can create a workaround for it, here is one for inspiration
from fastapi import FastAPI, Body, APIRouter, Request, Response
from typing import Callable
from fastapi.routing import APIRoute
from pydantic import BaseModel
import json
from ast import literal_eval
class CustomRoute(APIRoute):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
request_body = await request.body()
request_body = literal_eval(request_body.decode("utf-8"))
request_body = json.dumps(request_body).encode("utf-8")
request._body = request_body # Note that we are overriding the incoming request's body
response = await original_route_handler(request)
return response
return custom_route_handler
app = FastAPI()
router = APIRouter(route_class=CustomRoute)
class TestModel(BaseModel):
name: str
@router.post("/")
async def dummy(model: TestModel = Body(...)):
return model
app.include_router(router)
This is actually an invalid body
curl -X POST "http://127.0.0.1:8000/" -d "{ \"name\": \"st""ring\"}"
But with a custom route, we can make it work
Out: {"name":"string"}
Most helpful comment
Of course, it would fail, you can not use double quotes with the string that you created with double-quotes.
See, even syntax highlighting understands there is an error.
You can use an escape character
\Or you can use single quotes instead of double quotes