0
0

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.

FlaskでREST API CRUD作成。 Curlで結果表示。

Last updated at Posted at 2023-04-20

■完成

・左側画面:サーバーのリクエスト結果
・右側画面:コマンド & コマンド結果表示(Curl)

● post
スクリーンショット (701).png
● put
スクリーンショット (702).png
●delete
スクリーンショット (703).png

■ソースコード

python app.py

from flask import Flask, jsonify, request
app = Flask(__name__)

app.config['JSON_AS_ASCII'] = False  # JSONでの日本語文字化け対策

# 大本データ ※ここに、データを追加したり、削除したりしていく。
items = [{'id' : 1, 'name': 'tossy_01', 'price': 1024}, {'id':2, 'name': '夏目智徹', 'price': 8888888888}]

# (表示)GETメソッドで全てのアイテムを返すエンドポイント
@app.route('/items', methods=['GET'])
def get_items():
    return jsonify({'items': items})

#(登録) POSTメソッドで新しいアイテムを作成するエンドポイント 
@app.route('/items', methods=['POST'])
def create_item():
   # JSONを受け取る
    json = request.get_json()
    
    # JSONをパースする
    name = json['name']
    price = json['price']
    item = {'name': name, 'price': price}
    item_id = len(items) + 1
    items.append(item)
    
     # 返却用ディクショナリを構築
    item['id'] = item_id
    return jsonify(item)  # JSONをレスポンス


# (検索)GETメソッドで特定のアイテムを返すエンドポイント
@app.route('/items/<string:item_name>', methods=['GET'])
def get_item(item_name):
    for item in items:
        if item['name'] == item_name:
            return jsonify(item)
    return jsonify({'message': 'エラー 404.'}), 404

#(更新) PUTメソッドで特定のアイテムを更新するエンドポイント
@app.route('/items/<string:item_name>', methods=['PUT'])
def update_item(item_name):
    for item in items:
        if item['name'] == item_name:
            item['price'] = request.json['price']
            return jsonify(item)
    return jsonify({'message': 'エラー 404.'}), 404

#(削除)DELETEメソッドで特定のアイテムを削除するエンドポイント
@app.route('/items/<string:item_name>', methods=['DELETE'])
def delete_item(item_name):
    for item in items:
        if item['name'] == item_name:
            items.remove(item)
            return jsonify({'message': 'deleted OK'})
    return jsonify({'message': 'エラー 404.'}), 404

if __name__ == '__main__':
    app.run(debug=True)



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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?