python - Flask 端点的 RESTful 访问

标签 python json rest flask postman

我正在尝试在我的 Flask 应用程序中创建一个 RESTful 端点,但是当我尝试访问该端点时,它会返回:TypeError:organize() 恰好需要 1 个参数(给定 0 个)。我在 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>

最佳答案

我无法开始解释为什么会出现这种情况,但我发现通过使用 request.values 而不是 request.form 它能够访问正在发送的数据。

我仍然希望解释为什么 request.form 不起作用,但这确实解决了我的问题。

关于python - Flask 端点的 RESTful 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23325799/

相关文章:

javascript - Vue2 : JSON object using emit not working?

angular - 如何使用与angular2 http服务的保持连接

javascript - 我的 Javascript 仅适用于 Django 2 中嵌入 for 循环的 python 中的一个元素

python - 使用Python登录网站

python - 向量化搜索排序 numpy

javascript - 使用弹出窗口在 ANGULAR 中添加项目 json

java - 我想使用 GSON 的 fromJson() 将 json 对象转换为 java 对象

python - 在 PayPal 的 REST API 中执行付款后,有时我收不到回复电子邮件

java - WebTarget 和线程安全

python - 使用 pandas.eval 求和会为未包含在两个数据帧中的列带来 NaN 消息