我正在尝试发出发布请求,但出现错误405:不允许的方法。
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/register', methods=['POST'])
def register_user():
user_info = request.get_json()
if request.method == 'POST':
user = User.query.filter_by(username = user_info['username']).first()
if user is not None:
return jsonify({"msg":"User already exists!"}), 404
saved_user = User(username=user_info['username'], password=user_info['password'])
db.session.add(saved_user)
db.session.commit()
return ""
return "", 200
这是我的要求:
r = requests.post('https://myapp.herokuapp.com/register',json = {'username':'testname','password':'testpsw'})
assert r.status_code == 200
如果我将其更改为methods = ['GET', 'POST']
它只是得到。
更改以下行,
r = requests.post('https://myapp.herokuapp.com/register',json{'username':'testname','password':'testpsw'})
对此,
r = requests.post('http://localhost:5000/register', json = {'username':'testname','password':'testpsw'})
您需要将对象分配给request.post函数的第二个参数“ json”,请求才能正常工作。