diff --git a/sanic/request.py b/sanic/request.py index a5d204a8..3cc9c10b 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -178,6 +178,11 @@ class Request(dict): def content_type(self): return self.headers.get('Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + @property + def match_info(self): + """return matched info after resolving route""" + return self.app.router.get(self)[2] + @property def path(self): return self._parsed_url.path.decode('utf-8') diff --git a/tests/test_requests.py b/tests/test_requests.py index d61d4d55..2351a3b0 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -211,6 +211,19 @@ def test_content_type(): assert response.text == 'application/json' +def test_match_info(): + app = Sanic('test_match_info') + + @app.route('/api/v1/user//') + async def handler(request, user_id): + return json(request.match_info) + + request, response = app.test_client.get('/api/v1/user/sanic_user/') + + assert request.match_info == {"user_id": "sanic_user"} + assert json_loads(response.text) == {"user_id": "sanic_user"} + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ #