Skip to content
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

Is there a bug in the single body pass parameter? #11586

Closed
1 task done
ciaoyizhen opened this issue May 16, 2024 · 5 comments
Closed
1 task done

Is there a bug in the single body pass parameter? #11586

ciaoyizhen opened this issue May 16, 2024 · 5 comments

Comments

@ciaoyizhen
Copy link

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

server

from fastapi import FastAPI
import uvicorn
from pydantic import BaseModel

app = FastAPI()

class InputParams(BaseModel):
    name:str

@app.post("/")
async def index(params:InputParams):
    return f"hello {params.name}"


uvicorn.run(app)

client

import requests
import json

host = "http://localhost:8000"
uri = "/"
url = host + uri


body = {"name": "aaa"}

response = requests.post(url, json=body)
print(response.status_code)
print(response.json())

It's OK.
server

from fastapi import FastAPI, Body
import uvicorn

app = FastAPI()

@app.post("/")
async def index(name:str=Body(), age:int=Body()):
    return f"hello {name}, {age}"


uvicorn.run(app)

client

import requests
import json

host = "http://localhost:8000"
uri = "/"
url = host + uri


body = {"name": "aaa", "age":18}

response = requests.post(url, json=body)
print(response.status_code)
print(response.json())

OK again.

server

from fastapi import FastAPI, Body
import uvicorn
from pydantic import BaseModel

app = FastAPI()

class InputParams(BaseModel):
    name:str
    age:int

@app.post("/")
async def index(params:InputParams):
    return f"hello {params.name, params.age}, "


uvicorn.run(app)

client

import requests
import json

host = "http://localhost:8000"
uri = "/"
url = host + uri


body = {"name": "aaa", "age":18}

response = requests.post(url, json=body)
print(response.status_code)
print(response.json())

ok. No problem.

But!!!!
server

from fastapi import FastAPI, Body
import uvicorn

app = FastAPI()

@app.post("/")
async def index(name:str=Body()):
    return f"hello {name}"


uvicorn.run(app)

client

import requests
import json

host = "http://localhost:8000"
uri = "/"
url = host + uri


body = {"name": "aaa"}

response = requests.post(url, json=body)
print(response.status_code)
print(response.json())

It's failed. why??? very confuse!!!

How should I call it, or where can I get knowledge about it. will the next version of fastapi be able to support this call?
help me thank you very much!

@ciaoyizhen
Copy link
Author

422
{'detail': [{'type': 'string_type', 'loc': ['body'], 'msg': 'Input should be a valid string', 'input': {'name': 'aaa'}}]}

@ciaoyizhen
Copy link
Author

import requests
import json

host = "http://localhost:8000"
uri = "/"
url = host + uri

body = {"name": "aaa"}

response = requests.post(url, json="aaa")
print(response.status_code)
print(response.json())

this is correct. but why?

@ciaoyizhen
Copy link
Author

In fact, such a modification can be very problematic, as there are inconsistencies in calling the method when there is only one body and multiple bodies

@PhysicallyActive
Copy link

PhysicallyActive commented May 16, 2024

If you want to be able to only take one input parameter, you can do the following:

from fastapi import FastAPI, Body
import uvicorn
from typing import Annotated

app = FastAPI()

@app.post("/")
async def index(name: Annotated[str, Body(embed=True)]):
    return f"hello {name}"


uvicorn.run(app)

For a reference to the Body (in particular the embed) documentation:
https://fastapi.tiangolo.com/reference/parameters/?h=body+embed#fastapi.Body

When embed is True, the parameter will be expected in a JSON body as a key instead of being the JSON body itself.
This happens automatically when more than one Body parameter is declared.

@ciaoyizhen
Copy link
Author

thank you. It work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants