0

I have a test case using The test library from Django. In my test I have the following:

class TestLoadCurve(TestCase):

    def setUp(self):
        self.client = Client()
        self.test_user = User.objects.create_user('test_user', 'a@b.com', 'test_user')
        self.client.login(username="test_user", password="test_user")

    def test_post_loadcurve_as_xlsx(self):
    response = self.client.post(
        reverse('loadcurves-list'),
        {'file': open("app/tests/loadcurve_files/loadcurve.xlsx", "rb"), "name": "testuploadloadcurve"},
    )
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)

And the (simplified) endpoint looks like this:

class LoadCurveViewSet(
    mixins.CreateModelMixin,
    mixins.RetrieveModelMixin,
    mixins.ListModelMixin,
    viewsets.GenericViewSet
):
    ...

    def create(self, request, *args, **kwargs):
        up_file = request.FILES['file']
        ...
        return Response(status.HTTP_201_CREATED)

This test works. But I want to re-create the POST request that it makes using curl for some documentation. I have only made it so far:

curl --user <myuser>:<mypass> -X POST -H --data-urlencode "payload={\"file\": $(cat app/tests/loadcurve_files/loadcurve.xlsx), \"name\": \"curlloadcurve\"}" http://localhost:5100/loadcurves/

Using the debugger I am able to stop the processing at the line up_file = request.FILES['file'], at which point I realize that both request.FILES and request.data are both totally empty. Am I not making the POST request correctly? I was following this answer

Python version is 3.6.9 Django version is 3.1.3

wfgeo
  • 1,491
  • 3
  • 18
  • 35
  • The issue is not with Django, but your curl request – JPG Jan 05 '21 at 12:28
  • Yes, that's what I'm hoping to get help with. I just provided the Django code to establish context. – wfgeo Jan 05 '21 at 12:29
  • Does [this](https://stackoverflow.com/questions/12667797/using-curl-to-upload-post-data-with-files) answer your question? Note the `-F` imples form-encoded data being passed, which should be the case if your `file` is a FileField – Scratch'N'Purr Jan 05 '21 at 12:34

0 Answers0