Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I POST an array with multipart/form-data encoding?

Tags:

In a GET parameter string, or an "x-www-form-urlencoded" POST request, it's possible to specify an array of parameters by naming them with brackets (e.g. "name[]").

Is there a "correct" (or at least a wide-spread convention) way to specify an array of parameters with a "multipart/form-data" POST request?

Would the following be correct?

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

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

first index
--abc
Content-Disposition: form-data; name="name[]"

second index

If it varies by platform, I'm interested in the convention for Apache/PHP.

like image 935
DougW Avatar asked Apr 03 '12 23:04

DougW


People also ask

How do you send a multipart form data post?

With curl, you add each separate multipart with one -F (or --form ) flag and you then continue and add one -F for every input field in the form that you want to send. The above small example form has two parts, one named 'person' that is a plain text field and one named 'secret' that is a file.

How is multipart form data encoded?

Multipart/form-data is one of the most used enctype/content type. In multipart, each of the field to be sent has its content type, file name and data separated by boundary from other field. No encoding of the data is necessary, because of the unique boundary. The binary data is sent as it is.

How do you send a multipart in Fetch?

The basic idea is to use the FormData object (not supported in IE < 10): async function sendData(url, data) { const formData = new FormData(); for(const name in data) { formData. append(name, data[name]); } const response = await fetch(url, { method: 'POST', body: formData }); // ... }

What is multipart encoded?

enctype='multipart/form-data is an encoding type that allows files to be sent through a POST. Quite simply, without this encoding the files cannot be sent through POST. If you want to allow a user to upload a file via a form, you must use this enctype.


1 Answers

If you want an associated array you can pass index in a name of a form field:

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

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

first value
--abc
Content-Disposition: form-data; name="name[second]"

second value

Then on php level print_r($_POST) would give you

Array ( [name] => Array ( [first] => 'first value', [second] => 'second value' ) )

If you are after just a normal ordered array then same as you did:

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

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

first index
--abc
Content-Disposition: form-data; name="name[]"

second index

Then on php level print_r($_POST) would give you

Array ( [name] => Array ( [0] => 'first index', [1] => 'second index' ) )

Params with [] in their names translating into arrays on a server side is a feature specific to PHP (http://www.php.net/manual/en/faq.html.php#faq.html.arrays).

As for multipart encoding you can find more in RFC: http://www.ietf.org/rfc/rfc1867.txt

like image 84
Alexei Tenitski Avatar answered Oct 12 '22 22:10

Alexei Tenitski