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 '只接受post请求!
'
@app.route('/user/')
def user(name):
return'hello, %s
' % 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"}
leason|博客