FastAPI: HTTPie で使う
FastAPI の sqlite3 を使うサーバーを HTTPie で使う方法です。
サーバープログラムはこちらです。
SQL (Relational) Databases
ツリー構造
$ tree sql_app
sql_app
├── __init__.py
├── crud.py
├── database.py
├── main.py
├── models.py
└── schemas.py
サーバーの起動
uvicorn sql_app.main:app --reload
サーバーを起動すると
sql_app.db
というファイルが作成されます。
$ file sql_app.db
sql_app.db: SQLite 3.x database, last written using SQLite version 3035005
この時点でデータを見てみます。
$ http http://127.0.0.1:8000/users/
HTTP/1.1 200 OK
content-length: 2
content-type: application/json
date: Sat, 19 Mar 2022 03:37:46 GMT
server: uvicorn
[]
ユーザーを登録します。
http POST http://127.0.0.1:8000/users/ email=[email protected] password=hello
http POST http://127.0.0.1:8000/users/ email=[email protected] password=hello
http POST http://127.0.0.1:8000/users/ email=[email protected] password=hello
この時点でデータを見てみます。
$ http http://127.0.0.1:8000/users/
HTTP/1.1 200 OK
content-length: 190
content-type: application/json
date: Sat, 19 Mar 2022 03:39:24 GMT
server: uvicorn
[
{
"email": "[email protected]",
"id": 1,
"is_active": true,
"items": []
},
{
"email": "[email protected]",
"id": 2,
"is_active": true,
"items": []
},
{
"email": "[email protected]",
"id": 3,
"is_active": true,
"items": []
}
]
Item を入れてみます。
http POST http://127.0.0.1:8000/users/1/items/ title=weather description='Fine'
http POST http://127.0.0.1:8000/users/2/items/ title=weather description='Rain'
http POST http://127.0.0.1:8000/users/3/items/ title=weather description='Snow'
この時点でデータを見てみます。
$ http http://127.0.0.1:8000/users/
HTTP/1.1 200 OK
content-length: 370
content-type: application/json
date: Sat, 19 Mar 2022 03:41:24 GMT
server: uvicorn
[
{
"email": "[email protected]",
"id": 1,
"is_active": true,
"items": [
{
"description": "Fine",
"id": 1,
"owner_id": 1,
"title": "weather"
}
]
},
{
"email": "[email protected]",
"id": 2,
"is_active": true,
"items": [
{
"description": "Rain",
"id": 2,
"owner_id": 2,
"title": "weather"
}
]
},
{
"email": "[email protected]",
"id": 3,
"is_active": true,
"items": [
{
"description": "Snow",
"id": 3,
"owner_id": 3,
"title": "weather"
}
]
}
]
この時点の sql_app.db の状態
$ sqlite3 sql_app.db
SQLite version 3.35.5 2021-04-19 18:32:05
Enter ".help" for usage hints.
sqlite> .table
items users
sqlite> select * from users;
1|[email protected]|hellonotreallyhashed|1
2|[email protected]|hellonotreallyhashed|1
3|[email protected]|hellonotreallyhashed|1
sqlite> select * from items;
1|weather|Fine|1
2|weather|Rain|2
3|weather|Snow|3
sqlite> .quit
Author And Source
この問題について(FastAPI: HTTPie で使う), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/8992f9930fc49a2aaa74著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .