0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python+FastAPIからRequestsにより別APIを呼び出してみる。uvicornでホスティング

Last updated at Posted at 2022-11-02

PythonとFastAPIを使いAPIを作成し、
getで別のAPIを呼ぶサンプルです。

1.以下をpip installしておく
requests
FastAPI
uvicorn

2.main.pyファイルを作成し、以下を記載
※サンプルとして〒番号を取得するAPIを内部で呼び出している

main.py
import requests,json
from fastapi import FastAPI
app = FastAPI()

@app.get("/zipcode")
async def ZipCode(zipcode:int = 0):
    url = f'https://zipcloud.ibsnet.co.jp/api/search?zipcode={zipcode}'
    r = requests.get(url)
    print(r.text)
    return {"message":   json.loads(r.text)}

3.以下コマンドをmain.pyのある階層で実行

python3 -m uvicorn main:app --reload

4.実行結果
ParameterQueryに値を設定してGET
無事内容が返ってきました。

image.png

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?