0

I am trying to have my FastAPI accept a string as body. Note that this string is not a JSON. For me it is important that the body argument can also be specified in the SwaggerUI that FastAPI provides. At the moment, I am doing the following:

@app.put("items/{item_name}")
async def put(item_name: str, body: str = Body(..., media_type="text/plain")):
   print(body)

But it gives me an error: Error: Unprocessable Entity

guscht
  • 843
  • 4
  • 20
  • I tried you method about body, it's valid. My FastAPI is `0.80.0`. You can add more detail about you code. – Jedore Sep 24 '22 at 03:04
  • Please have a look at [this answer](https://stackoverflow.com/a/73761724/17865804), as well as [this answer](https://stackoverflow.com/a/70774077/17865804) and [this answer](https://stackoverflow.com/a/70636163/17865804). – Chris Sep 25 '22 at 05:15

1 Answers1

0

I hope it fixes:

Create your pydantic model:

from pydantic import BaseModel
class Item(BaseModel):
     item_name: str
     body: str

(if you create it in same page):

@app.put("items/{item_name}")
async def put(item_name: Item.item_name, body: Item.body):
   return body

Also it is better to use "id"s to update something.

Esref
  • 1
  • 2