Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

value_error.jsondecode Invalid control character at when posting multi line text in FastAPI #2281

Closed
MohashN opened this issue Nov 1, 2020 · 6 comments

Comments

@MohashN
Copy link

MohashN commented Nov 1, 2020

hello
this is one of my endpoints which is for posting(creating) new document

@app.post("/documents", response_model=Doc, status_code=status.HTTP_201_CREATED, tags=["Web Panel"])
def create_doc(doc: DocDef , token: str = Depends(verified_admin), db: Session = Depends(get_db)):
    pass
    `print("hi")`

my schemas are:

class DocDef(BaseModel):
    name: str
    doc: str


class Doc(DocDef):
    id: int
    
    class Config:
        orm_mode = True

but when I want to test a multi line text document I receive an error
request body :

{
  "name": "string",
  "doc": " this is test.
this is test too."
}

response body:

{
  "detail": [
    {
      "loc": [
        "body",
        46
      ],
      "msg": "Invalid control character at: line 3 column 25 (char 46)",
      "type": "value_error.jsondecode",
      "ctx": {
        "msg": "Invalid control character at",
        "doc": "{\n  \"name\": \"string\",\n  \"doc\": \" this is test.\nthis is test too.\"\n}",
        "pos": 46,
        "lineno": 3,
        "colno": 25
      }
    }
  ]
}

please help how can I fix this
regards

@MohashN MohashN changed the title value_error.jsondecode Invalid control character at when posting multi line text value_error.jsondecode Invalid control character at when posting multi line text in FastAPI Nov 1, 2020
@ycd
Copy link
Contributor

ycd commented Nov 1, 2020

Looks very similar to #1982, as @ArcLightSlavik said

In JSON a literal line break is not allowed inside a string, it needs to be replaced by \n

So when you want to write a multiline text just go like this

{
  "name": "string",
  "doc": " this is test.\nthis is test too."
}

@MohashN
Copy link
Author

MohashN commented Nov 1, 2020

Thanks for reply
So if I would like to post raw text, Is this only solution?

@ycd
Copy link
Contributor

ycd commented Nov 1, 2020

Yes, JSON doesn't allow breaking lines for readability.

@MohashN
Copy link
Author

MohashN commented Nov 1, 2020

Thanks again

@MohashN MohashN closed this as completed Nov 1, 2020
@tiangolo
Copy link
Owner

tiangolo commented Nov 9, 2022

Thanks for the help here @ycd! 🍰

And thanks for coming back to close the issue @MohashN

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7197 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants