Issue adding request headers for Django Tests

Question:

I need to add a header to a request in a Django test. I have browsed a few pages on both Stack Overflow and elsewhere, following various suggestions. I can add the headers to PUTs and GETs successfully, but having an issue with POSTs. Any advice would be greatly appreciated.

For PUT and GET I have used the following [passing successfully]:

resp = self.client.put(resource_url, res_params, **{'HTTP_SSL_CLIENT_CERT': self.client_cert})

For POST, I tried the same thing but am receiving the error:
“‘str’ object has no attribute ‘items'”

I have tried the following:

resp = self.client.post(resource_url, res_params, **{'HTTP_SSL_CLIENT_CERT': self.client_cert})

resp = self.client.post(resource_url, res_params, HTTP_SSL_CLIENT_CERT=self.client_cert)

resp = self.client.post(resource_url, res_params, HTTP_SSL_CLIENT_CERT='1234567890')
Asked By: carrera

||

Answers:

For anybody that finds themselves looking at this page with a similar issue, I was able to get this working with the following:

resp = self.client.post(resource_url, data=res_params, content_type='application/json', HTTP_SSL_CLIENT_CERT=self.client_cert)
Answered By: carrera

Note that you need to add the prefix HTTP_ to your header name

resp = self.client.post(resource_url,  HTTP_YOUR_CUSTOM_HEADER="your value")
Answered By: Yinon_90
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.