我试图在我的烧瓶应用中创建一个RESTful端点,但是当我尝试点击端点时,它返回:TypeError: organize() takes exactly 1 argument (0 given)
。我在chrome中使用Postman
来发送请求,并且我已将所有参数都包含为form-data
。
代码:
@app.route('/organize', methods = ['POST'])
def organize(request):
salary = request.form['salary']
monthly_expenses = 0.0
### Get city/state from zip or (if provided) just proceed
try:
zipcode = request.form['zipcode']
citystate = zippo(zipcode)
citystate = json.loads(citystate)
city = citystate['city']
state_abbr = citystate['state_abbr']
except DoesNotExist:
city = request.form['city']
state_abbr = request.form['state']
### Car stuff
paymentsbool = request.form['paymentsbool']
try:
cartype = request.form['cartype']
payments = request.form['payments']
insurance_year = request.form['insurance']
insurance = insurance_year / float(12)
except DoesNotExist:
trans = request.form['trans']
### Cell phone stuff
cellbool = request.form['cellbool']
try:
cell = request.form['cell']
monthly_expenses = monthly_expenses + cell
except DoesNotExist:
pass
### Living expenses
rent = request.form['rent']
nat = request.form['nat']
cable = request.form['cable']
gas = request.form['gas']
elec = request.form['elec']
groceries = request.form['groceries']
monthly_sal = salary / float(12)
monthly_sal_af_tax = monthly_sal * .72
monthly_savings = monthly_sal_af_tax * .15
if request.form['carbool'] == True:
car_month = car(cartype, payments, insurance)
monthly_bal = monthly_bal - car_month
transportation = car_month
else:
monthly_bal = monthly_bal - trans
transportation = trans
monthly_expenses = monthly_expenses + rent + nat + cable + gas + elec + groceries + transportation
monthly_bal = monthly_sal_af_tax - monthly_savings - monthly_expenses
return jsonify(monthly_bal = monthly_bal, transportation = transportation, monthly_sal_af_tax = monthly_sal_af_tax, monthly_savings = monthly_savings, monthly_expenses = monthly_expenses)
def car(type, payments, insurance):
gas_price = 4.00
cars = {1:10, 2:15, 3:20}
tank = cars[type]
gas_month = 2.00 * tank * gas_price
car_total = gas_month + payments + insurance
return car_total
我还想知道你是否使用jsonify
返回数据是最好的方法吗?感谢您的帮助,非常感谢。
编辑:
我还尝试用curl
命中端点,但它返回相同的结果。
curl -X POST -d {‘zipcode’:13905, ‘paymentsbool’:0, ‘trans’:10, ‘cellbool’:0, ‘rent’:20, ‘nat’:30, ‘cable’:40, ‘gas’:50, ‘elec’:60, ‘groceries’:70} http://127.0.0.1:8080/organize --header "Content-Type:application/json"
EDIT2:继续玩这个我已经删除了request
参数,以便该函数不带参数。当我尝试curl
调用时,它会返回
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
curl: (3) [globbing] unmatched close brace/bracket at pos 19
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
答案 0 :(得分:2)
我无法解释为什么会这样,但我发现通过使用request.values
代替request.form
,它可以访问正在发送的数据。
我仍然希望解释为什么request.form
无法正常工作,但这确实解决了我的问题。