Replies: 12 comments
-
In JSON a literal line break is not allowed inside a string, it needs to be replaced by \n
|
Beta Was this translation helpful? Give feedback.
-
@ArcLightSlavik |
Beta Was this translation helpful? Give feedback.
-
@BrambleXu Not sure what you mean. Fastapi settings won't deal with this, it's a json issue. |
Beta Was this translation helpful? Give feedback.
-
@ArcLightSlavik |
Beta Was this translation helpful? Give feedback.
-
Same issue I am facing with the double quotes in the request body. |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Hi ycd, in my use case, the raw text may contain double quotes. In those cases, JSON parser returns 422. Sample request body - Delimiting the double quotes in the raw-text I cannot control. |
Beta Was this translation helpful? Give feedback.
-
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"
} |
Beta Was this translation helpful? Give feedback.
-
Yes.. I know. Actually, I cannot control the input. And the user can put double quotes while making request to the API. |
Beta Was this translation helpful? Give feedback.
-
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
But with a custom route, we can make it work Out: {"name":"string"} |
Beta Was this translation helpful? Give feedback.
-
@BrambleXu if your problem is solved, do you mind closing the issue? |
Beta Was this translation helpful? Give feedback.
-
@BrambleXu kindly close the issue, since it was already mentioned that a literal line break inside a string isn't supported by JSON. As a note, you CAN pass raw text on your body to a fastapi endpoint, in which case you should set your Request Header 'Content-Type: plain/text' instead of using json. And it works after the fixes from #1018. Your endpoint could look something like:
Although I don't think this is what you wanted/needed, so most likely the already suggested option to include the \n char where you'd like the string field to have a line break is best. |
Beta Was this translation helpful? Give feedback.
-
Reproduce
I want to pass raw text to request body. But it has "422 Error: Unprocessable Entity" due to the
\n
Description
If there is text contains
\n
, it will throw the 422 error.Environment
Beta Was this translation helpful? Give feedback.
All reactions