Merge pull request #2400 from YunfanZhang42/fix_content_type

Handle exception with invalid HTTP content_type.
This commit is contained in:
Yunfan Zhang 2018-07-03 15:16:13 -04:00 committed by GitHub
commit e9a2100a62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -23,7 +23,7 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import views as auth_views
# Django REST Framework
from rest_framework.exceptions import PermissionDenied, AuthenticationFailed, ParseError, NotAcceptable
from rest_framework.exceptions import PermissionDenied, AuthenticationFailed, ParseError, NotAcceptable, UnsupportedMediaType
from rest_framework import generics
from rest_framework.response import Response
from rest_framework import status
@ -197,6 +197,10 @@ class APIView(views.APIView):
except (PermissionDenied, ParseError) as exc:
request.drf_request_user = None
self.__init_request_error__ = exc
except UnsupportedMediaType as exc:
exc.detail = _('You did not use correct content_type in your HTTP request. \
If you are using our REST API, the content_type must be application/json')
self.__init_request_error__ = exc
return drf_request
def finalize_response(self, request, response, *args, **kwargs):

View File

@ -107,3 +107,13 @@ def test_filterable_fields(options, instance, admin_user):
assert filterable_info['filterable'] is True
assert 'filterable' not in non_filterable_info
@pytest.mark.django_db
def test_handle_content_type(post, admin):
''' Tower should return 415 when wrong content type is in HTTP requests '''
post(reverse('api:project_list'),
{'name': 't', 'organization': None},
admin,
content_type='text/html',
expect=415)