Tompos Noel
Tompos Noel

Reputation: 31

Can I get URL parameter (set in url rule) with flask request?

I have this url route, can I get user_id with flask.request? I want to create a wrapper, and get the user_id here.

def test_required(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
    user_id = ?????

    return fn(*args, **kwargs)

return wrapper

@app.route('/api/test/<int:user_id>', methods=['GET'])
@test_required
def jwt_routes_test(user_id):

request.args, request.form, or request.values not return this value. Can I access it somehow?

Upvotes: 0

Views: 816

Answers (3)

hjpotter92
hjpotter92

Reputation: 80653

Looks like you're asking for view_args dictionary.

A dict of view arguments that matched the request. If an exception happened when matching, this will be None.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 600059

This code is only valid with a view function that accepts user_id as an argument. So, you simply get it from there.

Upvotes: 2

abdusco
abdusco

Reputation: 11151

Use it as a parameter in your route:

@app.route('/api/test/<int:user_id>', methods=['GET'])
def myroute(user_id: int):
    # do something

https://flask.palletsprojects.com/en/1.0.x/quickstart/#variable-rules

Upvotes: 1

Related Questions