2

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?

3
  • 1
    GET arguments are not passed as JSON body but as query string parameters in URL Commented Aug 17, 2020 at 11:39
  • But, it is okay to pass JSON payload in HTTP get requests, isn't it? Commented Aug 17, 2020 at 11:44
  • @ArakkalAbu Yes, it's ok in HTTP get request Commented Aug 17, 2020 at 11:52

3 Answers 3

4

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.