As Simon Willison mentions in his Debugging Django presentation, using the Test Client in the interpreter can be a great way to take a peek at the raw results from a view. In some cases you may need to add additional headers to the request (for instance a piece of middleware may rely on them).
Though it is not mentioned in the reference documentation, a quick peek at the code confirmed my hopes that it would be possible to add data to the request. The Client get and post methods both accept an extra kwargs parameter that allow you to populate the request with additional data.
1 2 3 4 5 6 7 8 9 10 11 12 13 | """
example of using the **extra kwarg param to add headers to the test client request
"""
from django.test.utils import setup_test_environment
setup_test_environment()
from django.test.client import Client
c = Client()
#example using a query string param
c.get('/some/path/', {'qs_param':'foo'}, **{'HTTP_USER_AGENT':'silly-human', 'REMOTE_ADDR':'127.0.0.1'})
#example without a query string param
c.get('/some/path/', **{'HTTP_USER_AGENT':'silly-human', 'REMOTE_ADDR':'127.0.0.1'})
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
response = self.client.post('/api/v2/chatbot/update-complete-flag/5cb81d51ae637b5cc6256f97/', {"data": {"chat_complete_flag": True, "bot_id": "5c864c61ae637b54fb596c96"}}, HTTP_AUTHORIZATION='Basic API-KEY:generatesomesecurekeyforthis')
Tried with above for HTTP_AUTHORIZATION header 'Malformed function' return
#
self.client.post('/api/v2/chatbot/update-complete-flag/5cb81d51ae637b5cc6256f97/', {"data": {"chat_complete_flag": True, "bot_id": "5c864c61ae637b54fb596c96"}}, **{'HTTP_AUTHORIZATION':'Basic QVBJLUtFWTpnZW5lcmF0ZXNvbWVzZWN1cmVrZXlmb3J0aGlz'})
tried with the above one too.
#
Please login first before commenting.