如何从post请求中获取数据?

问题描述 投票:0回答:1

我构建了一个 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 -

如何获取请求数据?

python flask
1个回答
0
投票
r = requests(url, json={"id":"good"})
© www.soinside.com 2019 - 2024. All rights reserved.