简体   繁体   中英

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

The parameters I am trying to get are code and state. The URL cannot be changed its an endpoint.

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
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
response = 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
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
raise value

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = 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
return 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
response = 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
"The view function did not return a valid response. The"

The view function did not return a valid response.

When accessing the defined route, a return value is expected. You can return JSON, simple strings or 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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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