33👍
✅
The Client.get()
method has an extra
keyword arguments parameter, which can be used to specify headers.
c.get(/my-url/, REMOTE_ADDR="127.0.0.1")
11👍
Pass REMOTE_ADDR in constructor.
client = HttpClient(REMOTE_ADDR='127.0.0.1')
or
client.get('/path/', {'param':'foo'}, **{'HTTP_USER_AGENT':'firefox-22', 'REMOTE_ADDR':'127.0.0.1'})
- [Django]-How to pass django rest framework response to html?
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
3👍
You can also set it for all future requests:
client.defaults['REMOTE_ADDR'] = '1.2.3.4'
Also with subclassing:
class DecoratedApiClient(Client):
def set_ip_addr(self, ip_addr):
self.defaults['REMOTE_ADDR'] = ip_addr
client = DecoratedApiClient()
client.set_ip_addr('1.2.3.4')
👤gak
- [Django]-Extend base.html problem
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-UUID as default value in Django model
-2👍
As simple like this:
client_address = request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR')
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Filtering using viewsets in django rest framework
- [Django]-Django models avoid duplicates
Source:stackexchange.com