We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Issue with request->getPut() when sending request as form-data encoded

When I send a PUT request to my Phalcon server with body encoded as form-data, fetching the data in the backend shows unexpected behaviour. For example:

I'm sending PUT with body as form-data:

PUT /test/phalconput/40 HTTP/1.1 Host: testphalconputserver.local Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW Authorization: Bearer avalidtoken Cache-Control: no-cache Postman-Token: 1ae28de5-bf7a-4b20-9156-548d077a4c1a

------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="param1"

value1 ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="param2"

value2 ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="param3"

In backend I fetch like this:

var_dump($this->request->getPut());

And the result is one array with one big string and alot of 'noise':

array (size=1) '----------------------------011430535261077664402366 Content-Disposition:_form-data;_name' => string '"param1"

value1 ----------------------------011430535261077664402366 Content-Disposition: form-data; name="param2"

value2 ----------------------------011430535261077664402366 Content-Disposition: form-data; name="param3"

The following:

$this->request->getPut('param1'); $this->request->getPut('param2');

return null. This is clearly unintended behaviour.

When you do the same with getPost you get a nice formatted result. A workaround for the getPut method is to send the request encoded as x-www-form-urlencoded. The problem with this encoding is you cannot send files easily.

Any way to resolve this without swithing to x-www-form-urlencoded, pasing that big string myself or changing the request to POST?

This is known issue. You need to do it like this:

json_decode($this->request->getPost(), true)

And you will get a whole thing.



2.7k
edited Jul '18

Thanks for answer. Did you mean to type getPost() in the above example?

Also, json decode expects a string, while getPost/getPut provides an array. And the array when calling getPut is an assoc array of size one with

key = '----------------------------011430535261077664402366 Content-Disposition:_form-data;_name'

value = '"param1"

value1 ----------------------------011430535261077664402366 Content-Disposition: form-data; name="param2"

value2 ----------------------------011430535261077664402366 Content-Disposition: form-data; name="param3"'