Reputation: 19838
I'm implementing an API for a legacy client which is not under my control.
The request is a POST which body is a JSON string.
Depending on the situation, there is no content type header, or the content type header is wrong.
I implemented the API. It's working fine with a CURL client that adds the application/json
content type. But it throws a 415 error when the content type is not provided. The problem is that I can't have the client add the correct content type.
How to force the view wrapped by @api_view(['POST'])
to use a JSONParser
no matter the headers of the request ?
Upvotes: 2
Views: 1199
Reputation: 33901
If you want to ditch the standard content negotiation and use something simpler, you can use a custom content negotiation class. The example given in the docs fits your needs.
class IgnoreClientContentNegotiation(BaseContentNegotiation):
def select_parser(self, request, parsers):
"""
Select the first parser in the `.parser_classes` list.
"""
return parsers[0]
You'll also want to adjust your settings, to use your custom class...
REST_FRAMEWORK = {
'DEFAULT_CONTENT_NEGOTIATION_CLASS':
'myapp.negotiation.IgnoreClientContentNegotiation',
}
Upvotes: 1