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

烂漫一生 提交于 2019-12-13 11:52:49

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10002821/how-do-i-post-an-array-with-multipart-form-data-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!