carrera
carrera

Reputation: 185

Issue adding request headers for Django Tests

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')

Upvotes: 2

Views: 2973

Answers (2)

Yinon_90
Yinon_90

Reputation: 1745

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")

Upvotes: 0

carrera
carrera

Reputation: 185

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)

Upvotes: 5

Related Questions