├── README.md └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # flask-api 2 | 3 | Simple Flask API to test GET, POST, DELETE and PUT request 4 | 5 | Clone this Repo 6 | 7 | ``` 8 | git clone git@github.com:kshyam/flask-api.git 9 | 10 | ``` 11 | 12 | ``` 13 | flask --debug run 14 | 15 | ``` 16 | 17 | Access form POSTMAN at 18 | 19 | ``` 20 | http://localhost:5000/api 21 | 22 | ``` 23 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request 2 | 3 | app = Flask(__name__) 4 | 5 | food_items = { "1":"rice", 6 | "2":"beans", 7 | "3":"yam", 8 | "4":"plantain", 9 | "5":"potatoes", 10 | "6":"wheat" 11 | } 12 | 13 | @app.route("/api") 14 | def index(): 15 | return "Hello form Flask API Server" 16 | 17 | @app.route('/data', methods = ['POST', 'GET']) 18 | def api(): 19 | if request.method == 'GET': 20 | return food_items 21 | 22 | if request.method =='POST': 23 | data = request.json 24 | food_items.update(data) 25 | return "Data is inserted" 26 | 27 | @app.route("/data/", methods=["PUT"]) 28 | def update(id): 29 | data = request.form['item'] 30 | food_items[str(id)]=data 31 | return "Data updated" 32 | 33 | @app.route("/data/", methods=["DELETE"]) 34 | def delete(id): 35 | food_items.pop(str(id)) 36 | return "Data Deleted" 37 | 38 | --------------------------------------------------------------------------------