Test Api with multiple files on same field

163 views
Skip to first unread message

Tobias

unread,
Dec 5, 2018, 7:00:49 AM12/5/18
to Django REST framework
Hello,

our api requires to upload multiple files on the same fieldname:

Content-Type: multipart/form-data;boundary=------FormBoundaryShouldDifferAtRuntime

------FormBoundaryShouldDifferAtRuntime
Content-Disposition: form-data; name="files"; filename="filenameA.pdf"
Content-Type: application/pdf

[content]
------FormBoundaryShouldDifferAtRuntime
Content-Disposition: form-data; name="files"; filename="filenameB.pdf"
Content-Type: application/pdf

[content]
------FormBoundaryShouldDifferAtRuntime--

At the moment i am not able to write a test with two files on the same files key. MultiValueDict does not work with APIClient.post(). Any recommendations?

Tobias

unread,
Jul 16, 2019, 5:20:49 AM7/16/19
to Django REST framework
Found the solution by myself: Just add a list of files to the files block.

from django.core.files.base import ContentFile
from rest_framework.test import APIClient

data = dict()
data['files'] = [
ContentFile(content='contentabc', name='filenameA.txt'),
ContentFile(content='contentxyz', name='filenameB.txt')
]

client = APIClient()
client.post('/testurl/', data=data, format='multipart')

Muhammad Imran

unread,
Oct 19, 2020, 2:36:29 AM10/19/20
to Django REST framework
Can you post serialize class and view here for upload images please I need to do that stuff big thanks.

Muhammad Imran

unread,
Oct 19, 2020, 5:06:52 AM10/19/20
to django-res...@googlegroups.com
Can you send me for one I need to do for one file

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/oT-3cJ8_bSE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/b97d5181-0801-445d-911f-36b210eb58f5n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages