Skip to content

๐Ÿ“จ ๐Ÿ’ฝ

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ‘ โ†ฉ๏ธ ๐ŸŽป, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Form.

Info

โš™๏ธ ๐Ÿ“จ, ๐Ÿฅ‡ โŽ python-multipart.

๐Ÿคถ โ“‚. pip install python-multipart.

๐Ÿ—„ Form

๐Ÿ—„ Form โšช๏ธโžก๏ธ fastapi:

from fastapi import FastAPI, Form

app = FastAPI()


@app.post("/login/")
async def login(username: str = Form(), password: str = Form()):
    return {"username": username}

๐Ÿ”ฌ Form ๐Ÿ”ข

โœ ๐Ÿ“จ ๐Ÿ”ข ๐ŸŽ ๐ŸŒŒ ๐Ÿ‘† ๐Ÿ”œ Body โš–๏ธ Query:

from fastapi import FastAPI, Form

app = FastAPI()


@app.post("/login/")
async def login(username: str = Form(), password: str = Form()):
    return {"username": username}

๐Ÿ–ผ, 1๏ธโƒฃ ๐ŸŒŒ Oauth2๏ธโƒฃ ๐Ÿ”ง ๐Ÿ’ช โš™๏ธ (๐Ÿค™ "๐Ÿ” ๐Ÿ’ง") โšซ๏ธ โœ” ๐Ÿ“จ username & password ๐Ÿ“จ ๐Ÿ‘.

๐Ÿ”Œ ๐Ÿšš ๐Ÿ‘ โšซ๏ธโ” ๐Ÿ“› username & password, & ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ‘, ๐Ÿšซ ๐ŸŽป.

โฎ๏ธ Form ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŽ ๐Ÿ“ณ โฎ๏ธ Body (& Query, Path, Cookie), ๐Ÿ”Œ ๐Ÿ”ฌ, ๐Ÿ–ผ, ๐Ÿ“› (โœ… user-name โ†ฉ๏ธ username), โ™’๏ธ.

Info

Form ๐ŸŽ“ ๐Ÿ‘ˆ ๐Ÿ˜– ๐Ÿ”— โšช๏ธโžก๏ธ Body.

Tip

๐Ÿ“ฃ ๐Ÿ“จ ๐Ÿ’ช, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Form ๐ŸŽฏ, โ†ฉ๏ธ ๐Ÿต โšซ๏ธ ๐Ÿ”ข ๐Ÿ”œ ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿ”ข โš–๏ธ ๐Ÿ’ช (๐ŸŽป) ๐Ÿ”ข.

๐Ÿ”ƒ "๐Ÿ“จ ๐Ÿ‘"

๐ŸŒŒ ๐Ÿ•ธ ๐Ÿ“จ (<form></form>) ๐Ÿ“จ ๐Ÿ’ฝ ๐Ÿ’ฝ ๐Ÿ›Ž โš™๏ธ "๐ŸŽ" ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ“Š, โšซ๏ธ ๐ŸŽ โšช๏ธโžก๏ธ ๐ŸŽป.

FastAPI ๐Ÿ”œ โš’ ๐Ÿ’ญ โœ ๐Ÿ‘ˆ ๐Ÿ“Š โšช๏ธโžก๏ธ โ–ถ๏ธ๏ธ ๐Ÿฅ‰ โ†ฉ๏ธ ๐ŸŽป.

๐Ÿ“ก โ„น

๐Ÿ“Š โšช๏ธโžก๏ธ ๐Ÿ“จ ๐Ÿ›Ž ๐Ÿ—œ โš™๏ธ "๐Ÿ“ป ๐Ÿ†Ž" application/x-www-form-urlencoded.

โœ‹๏ธ ๐Ÿ•โ” ๐Ÿ“จ ๐Ÿ”Œ ๐Ÿ“, โšซ๏ธ ๐Ÿ—œ multipart/form-data. ๐Ÿ‘† ๐Ÿ”œ โœ ๐Ÿ”ƒ ๐Ÿšš ๐Ÿ“ โญ ๐Ÿ“ƒ.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š โœ ๐ŸŒ– ๐Ÿ”ƒ ๐Ÿ‘‰ ๐Ÿ”ข & ๐Ÿ“จ ๐Ÿ‘, ๐Ÿ‘ณ ๐Ÿ‡ ๐Ÿ•ธ ๐Ÿฉบ POST.

Warning

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ’— Form ๐Ÿ”ข โžก ๐Ÿ› ๏ธ, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšซ ๐Ÿ“ฃ Body ๐Ÿ‘ ๐Ÿ‘ˆ ๐Ÿ‘† โŒ› ๐Ÿ“จ ๐ŸŽป, ๐Ÿ“จ ๐Ÿ”œ โœ”๏ธ ๐Ÿ’ช ๐Ÿ—œ โš™๏ธ application/x-www-form-urlencoded โ†ฉ๏ธ application/json.

๐Ÿ‘‰ ๐Ÿšซ ๐Ÿšซ FastAPI, โšซ๏ธ ๐Ÿ• ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ› ๏ธ.

๐ŸŒƒ

โš™๏ธ Form ๐Ÿ“ฃ ๐Ÿ“จ ๐Ÿ’ฝ ๐Ÿ”ข ๐Ÿ”ข.