2021年3月18日星期四

Passing text with \n in json request body gives unprocessable entity in fastapi

I am trying to pass a multi-line text to fastApi Code. My code looks like

from fastapi import FastAPI  from pydantic import BaseModel    from model import predictT5    testApp = FastAPI() # create an app    @testApp.get("/") # set root directory    # pydantic models  class TextIn(BaseModel):      text: str  class TextOut(BaseModel):      Summary: dict         # routes  def root():      return {"message":"jmj"}    @testApp.post("/predict", response_model=TextOut, status_code=200)  def get_prediction(payload: TextIn):      """      Gets a text and runs through T5 & outputs summary      """      text = payload.text      t5Summary = predictT5(text)  

This works with a request like

curl -X 'POST' \    'http://127.0.0.1:8000/predict' \    -H 'accept: application/json' \    -H 'Content-Type: application/json' \    -d '{    "text": "A dictionary that maps attention modules to devices. For reference, the t5 models have the following number of attention modules:"  }'  

However if I split the text with a \n and have a request body like

curl -X 'POST' \    'http://127.0.0.1:8000/predict' \    -H 'accept: application/json' \    -H 'Content-Type: application/json' \    -d '{    "text": "A dictionary that maps attention modules to devices.    For reference, the t5 models have the following number of attention modules:"  }'  

I see

Error: Unprocessable Entity "msg": "Invalid control character at: line 2 column 285 (char 286)", "type": "value_error.jsondecode",

Would anyone know how to handle this ? thank you!

https://stackoverflow.com/questions/66693302/passing-text-with-n-in-json-request-body-gives-unprocessable-entity-in-fastapi March 18, 2021 at 10:38PM

没有评论:

发表评论