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