0
Flask教程(二十七)json数据交互
source link: https://xugaoxiang.com/2022/12/05/flask-27-json/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- windows 10 64bit
- anaconda3 with python 3.8.11
- flask 2.2.2
json
是模块间数据传输最常见的方法,flask
同样能够很方便的接收和返回 json
格式的数据。
来看完整的服务端代码,这里使用 flask.request
中的 get_json
方法
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route('/', methods=['POST'])
def post():
# 获取请求的json数据
req_json = request.get_json()
print(req_json)
# 对接收到的数据进行简单处理
if req_json["operatorID"] != "0001":
return jsonify({"error": "error."})
dict_ret = {}
dict_ret["responseType"] = 2
dict_ret["status"] = 1000
dict_ret["num"] = 1
dict_ret["MD5"] = "4F3D2A1E"
return jsonify(dict_ret)
if __name__ == '__main__':
# 启动服务
app.run(host='0.0.0.0', port=80, debug=True)
然后,启动服务。我们使用 postman
这个工具来模型客户端请求
postman
发送请求后,可以得到响应,响应数据也是一个 json
如果请求 json
中 operatorID
不是 0001,服务端则返回 error
如果需要编程来实现请求,可以借助于 requests 这个库
import requests
r_json = {
"name": "xgx",
"operatorID":"0001",
"requestType":1,
"num":1
}
r_headers = {"Content-type": "application/json"}
r = requests.post('http://127.0.0.1', json=r_json, headers=r_headers)
print(r.status_code)
print(r.json())
https://github.com/xugaoxiang/FlaskTutorial
Flask系列教程
更多Flask
教程,请移步
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK