Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework : How to accept only json data?

I would like my Django rest framework API's to accept only Json data. So i set it in the default settings.

REST_FRAMEWORK = {
    ...
    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser',
    ],
}

but the views are still accepting the multipart form-data and x-www-form-urlencoded.

If i set the MultiPartParser in view than only multi form data is accepted , If i set the JSONParser in view than both multi form data and json is accepted.

How can i make it to accept only json data by default and multi-part only for a view in which i am uploading a file?

like image 294
Sachin Gupta Avatar asked Dec 07 '25 07:12

Sachin Gupta


1 Answers

If I'm comprehending it correct, you need to specify REST_FRAMEWORK variable in settings like this

REST_FRAMEWORK = {
    ...
    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser',
    ],

}
like image 71
hspandher Avatar answered Dec 08 '25 23:12

hspandher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!