2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

APIを雑に作る

Last updated at Posted at 2023-12-29

暇なのでAPIを作ってみる

  • 作るもの:ローランド判定API
  • 環境
    • MacBook Pro
    • Poetry+pyenv仮想環境

手順

以下の手順に従ってプロジェクトフォルダを作成
https://qiita.com/takumayagi/items/be00d0ae7932befcc6e5

main.pyにこんなことを書く

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "ローランド判定APIです"}

@app.post("/")
def sayHello(name: str):
    if name == "ローランド":
        return {"result": ""}
    else:
        return {"result": "俺以外"}

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

API起動

$ uvicorn main:app --reload

INFO:     Will watch for changes in these directories: ['/path/to/dev/dir']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [91718] using StatReload
INFO:     Started server process [91720]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

ブラウザでhttp://127.0.0.1:8000にアクセスする(='/'にGETする)

{"message":"ローランド判定APIです"}

でけた。

http://127.0.0.1:8000/docsにアクセスするとこんな画面が出現

image.png

試しにローランドと入力
image.png

こんな返答が返ってきた。いいぞ!

{
  "result": "俺"
}

ローランド以外を入力
image.png

{
  "result": "俺以外"
}

想定通り!
雑に作る、でした。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?