简体   繁体   English

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

[英]Why is @app.route('/<data>') ? not working?

I am trying to pass the data to a python flask server on the index/root url. 我正在尝试将数据传递到索引/根url上的python flask服务器。 Is there a way to do it or what am I doing wrong? 有没有办法做到这一点,或者我做错了什么?

The following is my code: 以下是我的代码:

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

When I run the command, it fails 当我运行命令时,它失败

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

The result from the python server python服务器的结果

"POST / HTTP/1.1" 404 -

The response from the curl command curl命令的响应

 <!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> 

RESOLVED WITH 解决

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

With request 有要求

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

Output was test 输出已测试

Your route defintion expects a request to /<somedata> . 您的路线定义期望对/<somedata>的请求。 If you omit <somedata> , it won't parse. 如果省略<somedata> ,则不会解析。

To POST json to / , do this: 为了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)

You can test that by doing this: 您可以通过执行以下操作进行测试:

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

To POST form-data to / , as you did in your example, do 为了POST表单数据/ ,因为你在你的例子一样,做

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

instead. 代替。 Test with 测试

$ curl -d 'foo=bar' localhost:5000/

To just pass a string to your route via urlquery, you can do: 要仅通过urlquery将字符串传递到路由,可以执行以下操作:

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

And test that with 并用

$ curl -X POST localhost:5000/somestring

You probably want to do: 您可能想做:

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

Although I am using blueprint.route to be honest, not app.route. 虽然我说的是blueprint.route,但不是app.route。

For me, this works fine. 对我来说,这很好。

@api.route('/api/v3/ticket/<string:key>/<int:id>'

its <type:var> combination, I believe. 我相信它的<type:var>组合。

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

Editing with more explanation. 编辑带有更多说明。 When I ran your problem. 当我跑你的问题。 like this 像这样

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

Then when you return data like this , 然后,当您返回这样的数据时,

@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 -

I guess, try a return at the end and it will work for you. 我想,最后尝试退货,这样对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM