大家好,又见面了,我是你们的朋友全栈君。
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_response
app = 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_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/<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_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/<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)
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/142568.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...