0

I'm currently trying to parse the multipart/form-data that I send through Postman plugin in Chrome. However, I get the following output:

'------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="album_id"

2
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="description"

haiahaahahahdaisdhisadhisadihsdhiiahsd
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="favorite"

true
------WebKitFormBoundarymsXhoqlRBbTbsZFb
Content-Disposition: form-data; name="uploadfile"; filename="test2.txt"
Content-Type: text/plain

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim  veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat  cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
 ------WebKitFormBoundarymsXhoqlRBbTbsZFb
 Content-Disposition: form-data; name="uploadfile"; filename="test.txt"
 Content-Type: text/plain

 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
 proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
 ------WebKitFormBoundarymsXhoqlRBbTbsZFb--
 '

This output do I get when I try to debug $this->request->input(). When I try $this->request->input('json_decode') I get an empty array, so I'm assuming that the data isn't in a proper format (just a string). Before I write my own algorithm I want to be sure that I'm not reinventing the wheel with this one. I'm doing something wrong? Or if not, does there exist some Cakephp 3 function which takes care of this?

Update
I found what is wrong in my code and it seems like the routing process somehow empties the post array to early. When I try Postman with the following endpoint http://vecto.app/api/pictures $this->request->data is empty. However, when I try to access the endpoint http://vecto.app/pictures the $this->request->data is filled with the information. Does anyone know what is wrong with the following routing setup:

Router::prefix('api', function ($routes) {
   $routes->extensions(['json', 'xml']);
   $routes->resources('Users');
   $routes->resources('Pictures');
   // We connect the /register action so we can simply extend the CRUD    Plugin add() method 
   // and benefit of already available logic like validation and response codes instead of having to reinvent the wheel.
   Router::connect('/api/users/register', ['controller' => 'Users',   'action' => 'add', 'prefix' => 'api']);
   $routes->fallbacks('InflectedRoute');
 });
markvdlaan93
  • 613
  • 10
  • 26
  • Even if CakePHP wouldn't provide methods to access form data (which [**it does**](http://book.cakephp.org/3.0/en/controllers/request-response.html#request-body-data)), why do you even think about trying to parse it on your own, when PHP already does that and exposes it via `$_POST` and `$_FILES`? – ndm Mar 04 '16 at 20:22
  • $this->request->data is empty when I try it through Postman ($this->request->is('post') actually outputs true). $_POST and $_FILES are also empty. I'm assuming that I'm doing something wrong but the strange thing is that $this->request->input() actually contains the snippet above. – markvdlaan93 Mar 04 '16 at 20:27
  • That should make you suspicious. Most likely your request is invalid/malformed. – ndm Mar 04 '16 at 20:32
  • I already posted something about it http://stackoverflow.com/questions/35780665/post-multipart-data-with-postman-to-cakephp . The answer with the screenshots is what I gave as input in Postman. I don't see anything wrong there. – markvdlaan93 Mar 04 '16 at 20:34
  • Enable debugging, check your logs. With a proper valid request, you wouldn't be able to see the raw request data by default, not for multipart requests (**http://php.net/manual/en/wrappers.php.php#wrappers.php.input**). As I said, your request is most likely invalid, like for example exceeding the allowed [**`post_max_size`**](http://php.net/manual/en/ini.core.php#ini.post-max-size), or maybe even [**`enable_post_data_reading`**](http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading) is disabled. – ndm Mar 04 '16 at 21:30
  • I tried several things. Still doesn't work but I tried just a normal form and it actually did output $this->request->data. If the post_max_size is small (I checked it, its default is set to 40 megabytes) or enable_post_data_reading is turned of then the form shouldn't work either right? – markvdlaan93 Mar 06 '16 at 21:05
  • Correct, neither should work (given that both run into the size restriction). – ndm Mar 06 '16 at 22:25
  • Okay, I already had a form that actually uploaded files before this whole problem started so I'm not sure what is going on here. It seems like when I choose multipart/form-data as content-type it just can't figure out how to put it in the $this->request->data container. I'm not sure how I can corrupt the data by just filling in the form-data in the Postman plugin. – markvdlaan93 Mar 06 '16 at 22:31
  • I can't tell you either, sending files with Postman works fine for me. – ndm Mar 06 '16 at 22:48
  • Do you may have some example code so that I can compare it with mine? (e.g. Postman headers/body and some controller method) – markvdlaan93 Mar 06 '16 at 22:54
  • I'm not doing anything special **http://i.imgur.com/epF1XMH.png** – ndm Mar 06 '16 at 23:19

1 Answers1

0

I finally found the solution which was a beforeFilter that emptied the $this->request->data array. Therefore, the input array wasn't empty but the data array was empty. This is the code that caused the problem:

public function beforeFilter(Event $event) { 
    if ($this->request->is('post')) { 
        $data = $this->request;
        $this->request->data = $this->request->input(function ($data) { 
            return json_decode($data, true); 
        }); 
    } 
}
markvdlaan93
  • 613
  • 10
  • 26