为什么@ app.route(&#39; / <data>&#39;)?不工作?

时间:2018-02-22 16:24:13

标签: python curl flask

我正在尝试将数据传递到索引/根URL上的python flask服务器。有没有办法做到这一点或我做错了什么?

以下是我的代码:

&#13;
&#13;
import json
from flask import Flask
app = Flask(__name__)

@app.route('/<data>', methods=['POST'])
def index(data):
	#test = request.args.get('test')
	return data
&#13;
&#13;
&#13;

当我运行命令时,它失败

curl  -d "data=123" http://localhost:9000/

python服务器的结果

"POST / HTTP/1.1" 404 -

curl命令的响应

&#13;
&#13;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
&#13;
&#13;
&#13;

决议

&#13;
&#13;
@app.route('/', methods=['POST'])
def index():
	return request.form.get('data')
&#13;
&#13;
&#13;

有了请求

curl  -d 'data=test' http://localhost:9000/

输出是测试

3 个答案:

答案 0 :(得分:0)

您的路线定义需要/<somedata>的请求。如果省略<somedata>,则无法解析。

POST json到/,请执行以下操作:

from flask import Flask, request, jsonify
app = Flask(__name__)


@app.route('/', methods=['POST'])
def index():
    data = request.get_json(force=True)
    return jsonify({'res':data})


if __name__ == '__main__':
    app.run(debug=True)

你可以通过这样做来测试:

$ curl -d '{"foo":"bar"}' localhost:5000/

POST表单数据到/,就像您在示例中所做的那样,

data = request.form['foo']
return data

代替。用

进行测试
$ curl -d 'foo=bar' localhost:5000/

要通过urlquery将字符串传递给您的路线,您可以执行以下操作:

@app.route('/<string:data>', methods=['POST'])
def index(data):
    return data

测试
$ curl -X POST localhost:5000/somestring

答案 1 :(得分:-1)

您可能想要这样做:

@app.route('/<data>', methods=['POST'])
def index(data):
    return json.dumps(data)

虽然我使用blueprint.route是诚实的,而不是app.route。

答案 2 :(得分:-1)

对我来说, 这很好用。

@api.route('/api/v3/ticket/<string:key>/<int:id>'
我相信它是<type:var>组合。

@app.route('/<data>', methods=['POST'])
def index(data):
    #test = request.args.get('test')
    print data

使用更多解释进行编辑。 当我运行你的问题。 像这样

127.0.0.1 - - [22/Feb/2018 22:17:29] "POST /haha HTTP/1.1" 200 -
 * Running on http://127.0.0.1:7000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 179-733-809
haha
127.0.0.1 - - [22/Feb/2018 22:16:50] "POST /haha HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask_restplus/api.py", line 557, in error_router
    return original_handler(e)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "/home/sworks/flask_application_new/flask-env/lib/python2.7/site-packages/flask/app.py", line 1725, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response

然后当你返回这样的数据时,

@app.route('/<data>', methods=['POST'])
def index(data):
    #test = request.args.get('test')
    return data



* Detected change in '/home/sworks/vms_temp/vms_enhancement/vms_api/smartworks/controllers_v1.py', reloading
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: 179-733-809
    127.0.0.1 - - [22/Feb/2018 22:17:29] "POST /haha HTTP/1.1" 200 -

我想,最后尝试一下,它会对你有用。