我正在从烧瓶休息的请求中捕获POST请求,并且我一直收到400错误代码(错误请求)。
请求部分:
def send_data(asset_id, data1, data2, data3):
global _server_api
headers = {'content-type': 'application/json'}
complete_url = _server_api + asset_id
payload = {'d1': data1,'d2': data2,'d3': data3}
response = requests.post(complete_url, data=json.dumps(payload), headers=headers)
Flask Part:
from flask.ext.restful.representations.json import output_json
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
assets = {}
class Broker(Resource):
def post(self, asset_id):
assets[asset_id] = dict()
data1 = request.form['d1']
data2 = request.form['d2']
data3 = request.form['d3']
assets[asset_id]['d1'] = request.form['d1']
assets[asset_id]['d2'] = request.form['d2']
assets[asset_id]['d3'] = request.form['d3']
collector(asset_id, data1, data2, data3) #<--- writes to DB
return {asset_id: assets[asset_id]}
api.add_resource(Broker, '/api-v1.0/add/<string:asset_id>', methods=['PUT', 'POST', 'GET'])
错误代码:
{u'status': 400, u'message': u'Bad Request'}
当请求部分调用API时,我一直收到400错误代码。
将 request.form 更改为 request.get_json 时,我收到如下错误。
TypeError: 'instancemethod' object has no attribute '__getitem__'
任何帮助将不胜感激。
答案 0 :(得分:1)
您正在发送JSON请求,但该应用需要一个表单。而不是I want to <Expr>double space.
Now highlight the <Expr> and set the text size to double the font you're using
在应用中使用request.form
。