diff --git a/sanic/request.py b/sanic/request.py index f3de36f8..a5d204a8 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -174,6 +174,10 @@ class Request(dict): # so pull it from the headers return self.headers.get('Host', '') + @property + def content_type(self): + return self.headers.get('Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + @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 06e2b7ae..d61d4d55 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -8,7 +8,7 @@ import pytest from sanic import Sanic from sanic.exceptions import ServerError from sanic.response import json, text - +from sanic.request import DEFAULT_HTTP_CONTENT_TYPE from sanic.testing import HOST, PORT @@ -192,6 +192,25 @@ def test_token(): assert request.token is None +def test_content_type(): + app = Sanic('test_content_type') + + @app.route('/') + async def handler(request): + return text(request.content_type) + + request, response = app.test_client.get('/') + assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE + assert response.text == DEFAULT_HTTP_CONTENT_TYPE + + headers = { + 'content-type': 'application/json', + } + request, response = app.test_client.get('/', headers=headers) + assert request.content_type == 'application/json' + assert response.text == 'application/json' + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ #