本文主要是介绍Flask---jsonify方式(api接口),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
-
- GET 方法
- post方法
- PUT 方法
- DELETE 方法
-
GET 方法
from flask import Flask, jsonify, abort, make_response
app = Flask(__name__)
articles = [{'id': 1,'title': 'the way to python','content': 'tuple, list, dict'},{'id': 2,'title': 'the way to REST','content': 'GET, POST, PUT'}
]
@app.route('/blog/api/articles', methods=['GET'])
def get_articles():""" 获取所有文章列表 """return jsonify({'articles': articles})@app.route('/blog/api/articles/<int:article_id>', methods=['GET'])
def get_article(article_id):""" 获取某篇文章 """article = filter(lambda a: a['id'] == article_id, articles)if len(article) == 0:abort(404)return jsonify({'article': article[0]})@app.errorhandler(404)
def not_found(error):return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':app.run(host='127.0.0.1', port=5632, debug=True)
http://127.0.0.1:5632/blog/api/articles输出如下:
{"articles": [{"content": "tuple, list, dict", "id": 1, "title": "the way to python"}, {"content": "GET, POST, PUT", "id": 2, "title": "the way to REST"}]
}
http://localhost:5632/blog/api/articles/2输出如下:
{"article": {"content": "GET, POST, PUT", "id": 2, "title": "the way to REST"}
}
当返回404错误时候,输出
{"error": "Not found"
}
post方法
from flask import request
from flask import Flask, jsonify, abort, make_responseapp = Flask(__name__)@app.route('/blog/api/articles', methods=['POST'])
def create_article():if not request.json or not 'title' in request.json:abort(400)article = {'id': 11,'title': request.json['title'],'content': request.json.get('content', '')}return jsonify({'article': article})if __name__ == '__main__':app.run(host='127.0.0.1', port=5632, debug=True)
输出如下:
PUT 方法
from flask import request
from flask import Flask, jsonify, abort, make_responseapp = Flask(__name__)articles = [{'id': 1,'title': 'the way to python','content': 'tuple, list, dict'},{'id': 2,'title': 'the way to REST','content': 'GET, POST, PUT'}
]
@app.route('/blog/api/articles/<int:article_id>', methods=['PUT'])
def update_article(article_id):article = list(filter(lambda a: a['id'] == article_id, articles))if len(article) == 0:abort(404)if not request.json:abort(400)article[0]['title'] = request.json.get('title', article[0]['title'])article[0]['content'] = request.json.get('content', article[0]['content'])return jsonify({'article': article[0]})if __name__ == '__main__':app.run(host='127.0.0.1', port=5632, debug=True)
DELETE 方法
from flask import request
from flask import Flask, jsonify, abort, make_responseapp = Flask(__name__)articles = [{'id': 1,'title': 'the way to python','content': 'tuple, list, dict'},{'id': 2,'title': 'the way to REST','content': 'GET, POST, PUT'}
]@app.route('/blog/api/articles/<int:article_id>', methods=['DELETE'])
def delete_article(article_id):article = list(filter(lambda t: t['id'] == article_id, articles))if len(article) == 0:abort(404)articles.remove(article[0])return jsonify({'result': True})if __name__ == '__main__':app.run(host='127.0.0.1', port=5632, debug=True)
这篇关于Flask---jsonify方式(api接口)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!