I have an API endpoint to which I want to make a PUT call which needs both a body and query parameters. I use Django's test client to call my endpoint in a test case (docs).
I read in the documentation that for a GET call, query parameters are introduced using the argument data
. I also read that for a PUT call, the argument data
represents the body. I miss documentation how to add query parameters in a PUT call.
In particular, this test case fails:
data = ['image_1', 'image_2']
url = reverse('images')
response = self.client.put(url,
data=data,
content_type='application/json',
params={'width': 100, 'height': 200})
And this test case passes:
data = ['image_1', 'image_2']
url = reverse('images') + '?width=100&height=200'
response = self.client.put(url,
data=data,
content_type='application/json')
In other words: is this manually URL building really necessary?
Assuming you're using rest_framework's
APITestClient
, I found this:whereas the put is:
and the interesting part (an excerpt from the
self.generic
code):so you could probably try to create that dict with
QUERY_STRING
and pass it toput
's kwargs, I'm not sure how worthy effort-wise that is though.