quikq
quikq

Reputation: 425

How to add headers to Django test requests?

how to use headers with django test requests?

I have requests like this:

requests.get(url=MY_URL, headers={"key": MY_KEY})
requests.post(url=MY_URL, json=MY_DATA, headers={"key": MY_KEY})

and it's working.

Right now I'm writing tests for some module and I have problem with key varialbe, because I've tried:

 self.client.get(MY_URL, **{"key": MY_KEY})
 self.client.get(MY_URL, headers={"key": MY_KEY})
 self.client.post(MY_URL, MY_DATA, **{"key": MY_KEY})
 self.client.post(MY_URL, MY_DATA, headers={"key": MY_KEY})

and all this requests return response 400. My guess is that there is something wrong with header. Do you know how to write this correctly?

Upvotes: 4

Views: 1392

Answers (2)

user20148142
user20148142

Reputation: 31

add the prefix HTTP_ to whatever objects you wanna pass. for example, if I want to pass "dog": "rexy" it will be a client.post("some_req", HTTP_dog="rexy")

Upvotes: 3

Mark Rofail
Mark Rofail

Reputation: 828

You should debug the prepared request and make sure your headers are in place correctly. I'm pretty sure your syntax is correct.

response = self.client.get(...)
print(response.request.__dict__)

Upvotes: 0

Related Questions