1

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.

Niels Uitterdijk
  • 713
  • 9
  • 27
  • You receive request.data in a POST request while request.query_params in a GET request. – SimbaOG Feb 18 '22 at 17:54
  • You may want to use `reverse` instead and format the url with that. Check https://stackoverflow.com/a/55202406/13239055 – Jack Feb 18 '22 at 17:57
  • 1
    response = client.delete('/comment', kwargs={'team': 0, 'id': 54}) – Emile Feb 18 '22 at 20:23

1 Answers1

1

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)}')

Niels Uitterdijk
  • 713
  • 9
  • 27