Format of arrays in multipart forms?

466 views
Skip to first unread message

ggi...@gmail.com

unread,
Aug 26, 2016, 5:53:31 PM8/26/16
to Django users
I'm trying to create an iOS app that can post multipart forms to Django. (JSON is trivial, wish it worked with file uploads. :-)

What is the proper format for the HTTP request when passing arrays of values? (Strings is all I care about at the moment)

I think PHP has the follow style. What is the Django equivalent? I tried reading through the sources, but it wasn't obvious.

Thanks,

     Greg

--Boundary-56A500AE-D543-43BD-93F0-6A1721A73F70

Content-Disposition: form-data; name="array[]"


one

--Boundary-56A500AE-D543-43BD-93F0-6A1721A73F70

Content-Disposition: form-data; name="array[]"


two

--Boundary-56A500AE-D543-43BD-93F0-6A1721A73F70

Content-Disposition: form-data; name="array[]"


three

--Boundary-56A500AE-D543-43BD-93F0-6A1721A73F70



Peter of the Norse

unread,
Sep 27, 2016, 10:15:45 PM9/27/16
to django...@googlegroups.com
PHP is stupid.  The problem with the [] syntax is that you have to be careful that the user hasn’t added or removed it from your fields.  This can cause problems if you were expecting an array and got a string, or vice-versa.

Instead, name the fields without [], and in your Django code, use request.POST.getlist(‘array’).  That way you are assured to get a list no matter what values the user sent.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4d999852-b1eb-4b39-bbb3-46606a77d0c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter of the Norse



Reply all
Reply to author
Forward
0 new messages