Django Rest Framework API Client Custom Header
Your initial solution is almost correct; you are just missing the "X" portion of your header field name:
self.client.credentials(HTTP_X_BULK_OPERATION='true')
This worked for me with other arbitrary header keys.
Or you can just pass it as a kwarg of request.
It is important to add HTTP_
prefix to your header (HTTP_MyHeader
, instead of MyHeader
):
response = self.client.patch(
'/api/v1/db_items/?active=True',
json.dumps(data),
content_type='application/json',
'HTTP_X_BULK_OPERATION'='true'
)
The answer is extremely poorly documented, but it seems django does its own parsing of the headers passed in. I successfully did this by changing my code to be
response = self.client.patch(
"/api/v1/db_items/?active=True",
json.dumps(data),
content_type="application/json",
HTTP_X_BULK_OPERATION="true",
)
Note the HTTP_
prefix. This is recognized by the Django client
and translated to a HTTP header with the key X-BULK-OPERATION
and the value "true"