Query parameters in PUT call of APIClient

4.2k Views Asked by At

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?

2

There are 2 best solutions below

1
On

Assuming you're using rest_framework's APITestClient, I found this:

def get(self, path, data=None, secure=False, **extra):
    """Construct a GET request."""
    data = {} if data is None else data
    r = {
        'QUERY_STRING': urlencode(data, doseq=True),
    }
    r.update(extra)
    return self.generic('GET', path, secure=secure, **r)

whereas the put is:

def put(self, path, data='', content_type='application/octet-stream',
        secure=False, **extra):
    """Construct a PUT request."""
    return self.generic('PUT', path, data, content_type,
                        secure=secure, **extra)

and the interesting part (an excerpt from the self.generic code):

    # If QUERY_STRING is absent or empty, we want to extract it from the URL.
    if not r.get('QUERY_STRING'):
        # WSGI requires latin-1 encoded strings. See get_path_info().
        query_string = force_bytes(parsed[4]).decode('iso-8859-1')
        r['QUERY_STRING'] = query_string
    return self.request(**r)

so you could probably try to create that dict with QUERY_STRING and pass it to put's kwargs, I'm not sure how worthy effort-wise that is though.

0
On

I just specify what @henriquesalvaro answer more detail.

You can pass query-parameters in PUT or POST method like below.

# tests.py
def test_xxxxx(self):
    url = 'xxxxx'
    res = self.client.put(url,**{'QUERY_STRING': 'a=10&b=20'})

# views.py
class TestViewSet(.....):

    def ...(self, request, *args, **kwargs):
        print(request.query_params.get('a'))
        print(request.query_params.get('b'))