我构建了一个 Pyton Flask 应用程序,如下所示:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from flask import Flask, request, jsonify, redirect
from flask_restful import Api, Resource, reqparse
import database_operators
import json
app = Flask(__name__)
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
api = Api(app)
@app.route('/task/<string:task_no>',methods=['get'])
def get_task(task_no):
db = database_operators.task_database()
task = db.get(task_no)
return jsonify(task)
@app.route('/task/create',methods=["post"])
def create_task():
print(request.data)
return "error"
if __name__ == '__main__':
app.run(debug=True)
我尝试像下面这样测试 create_task :
url = 'http://localhost:5000/task/create'
r = requests.post(url,data={"id":"good"})
print(r.text)
控制台只打印:
b''
127.0.0.1 - - [01/Dec/2024 15:07:35] "POST /task/create HTTP/1.1" 200 -
如何获取请求数据?
r = requests(url, json={"id":"good"})