https://blog.csdn.net/qq_29287973/article/details/52495042

flask实现简单的接收json返回json的接口
刚刚学习python想用python flask实现简单的接收json返回json的接口,微型的前后端分离接口,非常简单直接上代码。
结合http://docs.jinkan.org/docs/flask/quickstart.html#a-minimal-application这个flask文档实现

#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from flask import Flask
from flask import request
from flask import redirect
from flask import jsonify
app = Flask(__name__)

@app.route('/' , methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        a = request.get_data()
        dict1 = json.loads(a)
        return json.dumps(dict1["data"])
    else:
        return '<h1>只接受post请求!</h1>'

@app.route('/user/<name>')
def user(name):
    return'<h1>hello, %s</h1>' % name

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

请求数据为{
“opr”: “add”,
“data”: {
“userName”: “98997”,
“disc”: “hudihiudhu”,
“expDate”:”2”,
“ip”: [
“10.10.11.1”,
“10.10.11.2”,
“10.10.11.3”
]
}
}
返回{“userName”: “98997”, “ip”: [“10.10.11.1”, “10.10.11.2”, “10.10.11.3”], “disc”: “hudihiudhu”, “expDate”: “2”}

文档更新时间: 2019-06-16 20:50   作者:月影鹏鹏