Zeus
Zeus

Reputation: 83

How to send data in GET request in django rest api test

There are "Consultant" and "Price" tables in DB and "Price" has a foreign key to "Consultant". I want to get all price records that are related to specific consultant. But I get an error when I use APITestCase to send GET request.

views.py:

class PriceAPI(APIView):
    serializer_class = PriceSerializer
    def get(self, request):
        consultant_type = request.data.get('type', None)
        try:
            consultant = Consultant.objects.get(user=request.user, type=consultant_type)
        except Consultant.DoesNotExist:
            return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
        try:
            serializer = self.serializer_class(consultant.prices, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except:
            return Response(status=status.HTTP_400_BAD_REQUEST)

test.py:

class PriceTest(APITestCase):
    def setUp(self):
        ###
    def test_get_delete_price(self):
        response = self.client.get(
            reverse('price'),
            data=json.dumps(
                {'type': 'sports'}),
            content_type='application/json'
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)

I get this error:

Error
Traceback (most recent call last):
  File "D:\programming\Pycharm\Projects\Django Projects\CAPP\capp_api\tests.py", line 394, in test_get_delete_price
    content_type='application/json'
  File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\rest_framework\test.py", line 286, in get
    response = super().get(path, data=data, **extra)
  File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\rest_framework\test.py", line 194, in get
    'QUERY_STRING': urlencode(data or {}, doseq=True),
  File "D:\programming\Pycharm\Projects\Django Projects\CAPP\venv\lib\site-packages\django\utils\http.py", line 113, in urlencode
    for key, value in query:
ValueError: not enough values to unpack (expected 2, got 1)

This error is about data that is sent in request. How can I do that in GET request?

Upvotes: 2

Views: 4293

Answers (3)

Ilija
Ilija

Reputation: 131

Instead of =>

payload = {"uid": "123456"}
result = client.get(
         "/records/get-ownership-data/", data=payload
        ) 

You have option to format directly with f-string=>

uid_value = "123456"
result = client.get(
            f"/records/get-ownership-data/?uid={uid_value}"
        )

and then to read:

 result = request.GET.get("uid_value", None)

I think it's more about readability. Second option is more explicit about fact that we are adding like query parameter.

Upvotes: 0

Zeus
Zeus

Reputation: 83

GET request can't pass JSON body. Parameters can pass by query string. So in test.py data must change and in "get" function in views.py by request.GET.get('field') we can access to that parameter.

test.py:

response = self.client.get(
    reverse('price'),
    data={'type': 'sports'},  # This line is changed
    content_type='application/json'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

views.py:

consultant_type = request.GET.get('type', None)

Upvotes: 4

Chris Curvey
Chris Curvey

Reputation: 10389

this:

'QUERY_STRING': urlencode(data or {}, doseq=True),

makes me think that data should be a dictionary, but you're using json.dumps() to convert it to a string.

Upvotes: 0

Related Questions