Search code examples
djangodjango-rest-frameworkpytest

DRF APIClient Delete data arrives in request.data, not request.query_params


I use DRF's APIClient to write automated tests. And while is was writing the first delete test, I found it very strange that the data passed through arrived in request.data, while if I use Axios or Postman, it always arrives in request.query_params. Any explanation as to why this is, and preferably a method to use APIClient.Delete while the data arrives in query_params would be great!

My test:

import pytest
from rest_framework.test import APIClient


@pytest.fixture()
def client():
    client = APIClient()
    client.force_authenticate()
    yield client


class TestDelete:

    def test_delete(client):
        response = client.delete('/comment', data={'team': 0, 'id': 54})

And my views

from rest_framework.views import APIView



class Comments(APIView):

    def delete(self, request):
        print(request.query_params, request.data)


>>> <QueryDict: {}> <QueryDict: {'team': ['0'], 'id': ['54']}>

Looked into DRF's APIClient. Feeding towards params doesn't seem to help. The delete method doesn't seem to have direct arguments that could help as well. So I'm a bit stuck.


Solution

  • Though some good options have been proposed, they didn't work with DRF's APIView. I ended up using urllib and encode it manually:

    import pytest
    from urllib.parse import urlencode
    from rest_framework.test import APIClient
    
    
    @pytest.fixture()
    def client():
        client = APIClient()
        client.force_authenticate()
        yield client
    
    
    class TestDelete:
    
        def test_delete(client):
            response = client.delete(f'/comment{urlencode(data)}')