简体   繁体   English

如何在 FLASK 中将 url 作为 REST GET 参数传递

[英]How do you pass a url as a REST GET parameter in FLASK

So this is my code.所以这是我的代码。

from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
app.config['DEBUG'] = True
api = Api(app)

# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app


class Default(Resource):
   def get(self, name):
    """Renders a sample page."""
    return "Hello " + name

class LiveStats(Resource):
  def get(self, url):
    return "Trying to get " + url

    # data = request.get(url)
    # return data

api.add_resource(Default, '/default/<string:name>') # Route_1
api.add_resource(LiveStats, '/liveStats/<path:url>') # Route_2

if __name__ == '__main__':
 import os
HOST = os.environ.get('SERVER_HOST', 'localhost')    
try:
    PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
    PORT = 5555
app.run(HOST, PORT)

Now firstly this post helped a lot.现在首先这篇文章有很大帮助。 how-to-pass-urls-as-parameters-in-a-get-request-within-python-flask-restplus how-to-pass-urls-as-parameters-in-a-get-request-within-python-flask-restplus

Changing what I originally had.改变我原来拥有的。

 api.add_resource(LiveStats, '/liveStats/<string:url>') # Route_2  

to this对此

api.add_resource(LiveStats, '/liveStats/<path:url>') # Route_2  

got rid of 404 errors that I had but now I am noticing that it's not passing all of the url.摆脱了我遇到的 404 错误,但现在我注意到它没有传递所有的 url。

Example if I try this例如,如果我尝试这个

localhost:60933/liveStats/http://address/Statistics?NoLogo=1%26KSLive=1

I get this我明白了

Trying to get http://address/Statistics

so it has taken off ?NoLogo=1%26KSLive=1所以它已经起飞了?NoLogo=1%26KSLive=1

How do you prevent this?你如何防止这种情况?

All characters after the ? ?之后的所有字符are considered parameters.被视为参数。 From the docs:从文档:

To access parameters submitted in the URL (?key=value) you can use the args attribute:要访问在 URL (?key=value) 中提交的参数,您可以使用 args 属性:

searchword = request.args.get('key', '')

We recommend accessing URL parameters with get or by catching the KeyError because users might change the URL and presenting them a 400 bad request page in that case is not user friendly.我们建议使用 get 或通过捕获 KeyError 来访问 URL 参数,因为用户可能会更改 URL 并向他们呈现 400 错误的请求页面,在这种情况下对用户不友好。

For a full list of methods and attributes of the request object, head over to the Request documentation.有关请求对象的方法和属性的完整列表,请转到请求文档。

Maybe you could encode the query string in a way that you can retrieve it as a single parameter on the back end, but not sure it would be useful.也许您可以对查询字符串进行编码,以便在后端将其作为单个参数进行检索,但不确定它是否有用。

If you don't want to access the args individually, you can access the full query string:如果您不想单独访问 args,则可以访问完整的查询字符串:

request.query_string

Putting this all together I think this will work for you:把这一切放在一起,我认为这对你有用:

class LiveStats(Resource):
  def get(self, url):
    return "Trying to get " + url + request.query_string

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

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