400 Bad Request: The browser (or proxy) sent a request that this server coul

错误类型

[POST]>400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

400 (错误请求) 服务器不理解请求的语法。

使用场景flask

后台报400错误。400错误表示 由于语法格式有误,服务器无法理解此请求。
但后台的接口确实可以进行post请求。之后发现原因是当flask的request.form无法获取到对应的参数时,就会报400错误。
使用post请求时, 若是参数为json数据, request。

更正总结

json数据不能用表单接收, 表单最好也不要接收json数据

Flask 中接收与发送表单方式

from flask import Flask,render_template,request

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        print username,password
    return render_template('login.html',method=request.method)

Flask 中接收与发送json 方式详解

(转)https://blog.csdn.net/gymaisyl/article/details/86629874

1、利用flask的request.form.get()方法接收

from flask import Flask
from flask import jsonify
from flask import request
import json
 
@app.route("/login", methods=['POST'])
def login():
	 data_ = request.form.get('data')
	 data = json.loads(data)
	 username = data['username']
	 password = data['password']

	 return jsonify({"login": Login.login(username, password)})  # 返回布尔值

2、 利用flask的request.get_data()方法接收

from flask import Flask
from flask import jsonify
from flask import request
import json

@app.route("/flask/login", methods=['POST'])
def login():
 data = request.get_data()
 data = json.loads(data)
 username = data['username']
 password = data['password']

 return jsonify({"login": Login.login(username, password)}) # 返回布尔值

3、利用flask的request.get_json()方法接收

from flask import Flask
from flask import jsonify
from flask import request


@app.route("/flask/login", methods=['POST'])
def login():
 data = request.get_json()
 username = data['username']
 password = data['password']
 
 return jsonify({"login": Login.login(username, password)}) # 返回布尔值

你可能感兴趣的:(Flask)