-
|
Is there a way to define JSON body with only single key using import uvicorn
from fastapi import Body, FastAPI
app = FastAPI()
@app.post("/good")
async def good(name: str = Body(...), surname: str = Body(...)):
return {"name": name, "surname": surname}
uvicorn.run(app, host='127.0.0.1', port=8000)$ curl -X POST "http://127.0.0.1:8000/good" -d '{"name": "John", "surname": "Doe"}'
{"name":"John","surname":"Doe"}But I couldn't make it work for body with only one key: @app.post("/bad")
async def bad(name: str = Body(...)):
return {"name": name}$ curl -X POST "http://127.0.0.1:8000/bad" -d '{"name": "John"}'
{"detail":[{"loc":["body"],"msg":"str type expected","type":"type_error.str"}]}Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
You are. Try this: $ curl -X POST "http://127.0.0.1:8000/bad" -d '{"John"}' |
Beta Was this translation helpful? Give feedback.
-
|
@Kludex thanks but it doesn't work either. I have found an answer here: @app.post("/bad")
async def bad(name: str = Body(..., embed=True)):
return {"name": name}$ curl -X POST "http://127.0.0.1:8000/bad" -d '{"name": "John"}'
{"name":"John"} |
Beta Was this translation helpful? Give feedback.
-
|
Sorry, I meant (without the brackets) : 😅 $ curl -X POST "http://127.0.0.1:8000/bad" -d '"John"' But you can also use the embed, I just wanted to show you that it's not wrong, you just expect a different input format. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @Kludex ! 👏 🙇 Thanks for reporting back and closing the issue @drnextgis 👍
|
Beta Was this translation helpful? Give feedback.
Sorry, I meant (without the brackets) : 😅
But you can also use the embed, I just wanted to show you that it's not wrong, you just expect a different input format.