简体   繁体   English

从端点获取参数 python flask

[英]Getting a parameter from an endpoint python flask

I am trying to get 2 parameters from this URL http://127.0.0.1:5000/login/code=EULPK3PWJC1OLDY16UCLDKEZGUDLXUOYMP&state=342725139626065920我正在尝试从此 URL http://127.0.0.1:5000/login/code=EULPK3PWJC1OLDY16UCLDKEZGUDLX053926=069276获取 2 个参数

The parameters I am trying to get are code and state.我要获取的参数是代码和 state。 The URL cannot be changed its an endpoint. URL 不能更改其端点。

So far I came up with this code but it's not working:到目前为止,我想出了这段代码,但它不起作用:

@app.route("/login/<string:code><int:state>", methods=['GET'])
def login(code, state):
    print(code, state)

and I am getting this error我收到了这个错误

Traceback (most recent call last):回溯(最近一次通话最后):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2464, in call文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 2464 行,调用
return self.wsgi_app(environ, start_response) return self.wsgi_app(environ, start_response)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2450, in wsgi_app wsgi_app 中的文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 2450 行
response = self.handle_exception(e)响应 = self.handle_exception(e)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1867, in handle_exception文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 1867 行,在 handle_exception
reraise(exc_type, exc_value, tb) reraise(exc_type, exc_value, tb)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_compat.py", line 39, in reraise文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_compat.py”,第 39 行,在 reraise
raise value升值

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2447, in wsgi_app wsgi_app 中的文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 2447 行
response = self.full_dispatch_request()响应 = self.full_dispatch_request()

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64>\lib\site-packages\flask\app.py", line 1953, in full_dispatch_request文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64>\lib\site-packages\flask\app.py”,第 1953 行,在 full_dispatch_request
return self.finalize_request(rv)返回 self.finalize_request(rv)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1968, in finalize_request文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 1968 行,在 finalize_request
response = self.make_response(rv)响应 = self.make_response(rv)

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2098, in make_response文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py”,第 2098 行,在 make_response
"The view function did not return a valid response. The" “视图 function 未返回有效响应。”

The view function did not return a valid response.视图 function 未返回有效响应。

When accessing the defined route, a return value is expected.访问定义的路由时,需要返回值。 You can return JSON, simple strings or HTML (render_template).您可以返回 JSON、简单字符串或 HTML (render_template)。

Example for that:示例:

@app.route("/login/<string:code><int:state>", methods=['GET'])
def login(code, state):
    return {"Code": code, "State": state}

EDIT:编辑:

@app.route("/login/", methods=['GET'])
def login():
   code = request.args.get('code')
   state = request.args.get('state')
   return {"Code": code, "State": state}

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

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