Validate Content-Type header for REST API requests #6818

Closed
opened 2025-12-29 19:45:45 +01:00 by adam · 1 comment
Owner

Originally created by @jeremystretch on GitHub (Aug 16, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.2.9

Feature type

New functionality

Proposed functionality

Implement logic to check that the Content-Type header for inbound REST API requests has been properly set to application/json, and return an error if missing or invalid.

Use case

Currently, if a REST API request is made to create an object without specifying the correct content type, validation will fail reporting missing required fields. This is because the serializer (which expects JSON data) is not properly processing the request. This can be very confusing to the user.

Database changes

No response

External dependencies

No response

Originally created by @jeremystretch on GitHub (Aug 16, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.2.9 ### Feature type New functionality ### Proposed functionality Implement logic to check that the `Content-Type` header for inbound REST API requests has been properly set to `application/json`, and return an error if missing or invalid. ### Use case Currently, if a REST API request is made to create an object without specifying the correct content type, validation will fail reporting missing required fields. This is because the serializer (which expects JSON data) is not properly processing the request. This can be very confusing to the user. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 19:45:45 +01:00
adam closed this issue 2025-12-29 19:45:45 +01:00
Author
Owner

@jeremystretch commented on GitHub (Aug 18, 2022):

One way to approach this would be to omit any parsers other than JSONParser in the DRF settings:

    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser',
        # 'rest_framework.parsers.FormParser',
        # 'rest_framework.parsers.MultiPartParser'
    ],

This triggers a 415 error if the Content-Type header is not set:

{
    "detail": "Unsupported media type \"application/x-www-form-urlencoded\" in request."
}
@jeremystretch commented on GitHub (Aug 18, 2022): One way to approach this would be to omit any parsers other than `JSONParser` in the DRF settings: ```python 'DEFAULT_PARSER_CLASSES': [ 'rest_framework.parsers.JSONParser', # 'rest_framework.parsers.FormParser', # 'rest_framework.parsers.MultiPartParser' ], ``` This triggers a 415 error if the `Content-Type` header is not set: ``` { "detail": "Unsupported media type \"application/x-www-form-urlencoded\" in request." } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6818