Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 269
quikq Avatar asked Oct 11 '25 08:10

quikq


1 Answers

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

like image 86
user20148142 Avatar answered Oct 14 '25 05:10

user20148142



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!