Post multipart/data with Postman to Cakephp
<p>I'm currently building a RESTful API in Cakephp 3. I'm trying to upload a file (with some additional data) through form-data in the Postman chrome extension. However, I'm not really sure how I can obtain the data that is received through the request.</p>

<p><a href="http://i.stack.imgur.com/M6sD3.png" rel="nofollow">Postman form-data</a></p>

<p>This is the data that I try to send to the pictures controller in the api folder with the following code:</p>

<pre><code>  public function add(){
    debug($this-&gt;request-&gt;data);
    $picture = $this-&gt;Pictures-&gt;newEntity($this-&gt;request-&gt;data);
    if ($this-&gt;Pictures-&gt;save($picture)) {
        $message = 'Saved';
    } else {
        $message = 'Error';
    }
    $this-&gt;set([
        'message' =&gt; $message,
        'picture' =&gt; $picture,
        '_serialize' =&gt; ['message', 'picture']
    ]);
   }
</code></pre>

<p>This throws a database error which says that I'm violating a foreign key constraint (I think this is because <code>$this-&gt;request-&gt;data</code> is empty). However, I only want to see how the posted data is looking but the <code>$this-&gt;request-&gt;data</code> doesn't show up when I try to output it with the <code>debug()</code> function. I tried to post a JSON body which actually does work. My question is how to get the data in the controller that is send through form-data.</p>

<p><strong>edit</strong>
I did managed to output the request data which is indeed empty but <code>$this-&gt;request-is('post')</code> is true. When I try to output <code>$this-&gt;request</code> with <code>debug()</code> I get the following information:</p>

<pre><code>object(Cake\Network\Request) {
params =&gt; [
    'plugin' =&gt; null,
    'controller' =&gt; 'Pictures',
    'action' =&gt; 'add',
    '_ext' =&gt; null,
    'pass' =&gt; [],
    'prefix' =&gt; 'api',
    'isAjax' =&gt; false
]
data =&gt; null
query =&gt; []
cookies =&gt; []
url =&gt; 'api/pictures/add'
base =&gt; ''
webroot =&gt; '/'
here =&gt; '/api/pictures/add'
trustProxy =&gt; false
[protected] _environment =&gt; [
    'USER' =&gt; 'www-data',
    'HOME' =&gt; '/var/www',
    'FCGI_ROLE' =&gt; 'RESPONDER',
    'QUERY_STRING' =&gt; '',
    'REQUEST_METHOD' =&gt; 'POST',
    'CONTENT_TYPE' =&gt; 'multipart/form-data',
    'CONTENT_LENGTH' =&gt; '375',
    'SCRIPT_NAME' =&gt; '/index.php',
    'REQUEST_URI' =&gt; '/api/pictures/add',
    'DOCUMENT_URI' =&gt; '/index.php',
    'DOCUMENT_ROOT' =&gt; '/home/vagrant/Apps/vecto.app/webroot',
    'SERVER_PROTOCOL' =&gt; 'HTTP/1.1',
    'GATEWAY_INTERFACE' =&gt; 'CGI/1.1',
    'SERVER_SOFTWARE' =&gt; 'nginx/1.7.9',
    'REMOTE_ADDR' =&gt; '10.33.10.1',
    'REMOTE_PORT' =&gt; '55842',
    'SERVER_ADDR' =&gt; '10.33.10.10',
    'SERVER_PORT' =&gt; '80',
    'SERVER_NAME' =&gt; 'vecto.app',
    'REDIRECT_STATUS' =&gt; '200',
    'SCRIPT_FILENAME' =&gt; '/home/vagrant/Apps/vecto.app/webroot/index.php',
    'HTTP_HOST' =&gt; 'vecto.app',
    'HTTP_CONNECTION' =&gt; 'keep-alive',
    'HTTP_CONTENT_LENGTH' =&gt; '375',
    'HTTP_ACCEPT' =&gt; 'multipart/form-data',
    'HTTP_POSTMAN_TOKEN' =&gt; 'be1a629c-d81e-b19b-4e0f-294a46779150',
    'HTTP_CACHE_CONTROL' =&gt; 'no-cache',
    'HTTP_ORIGIN' =&gt; 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
    'HTTP_AUTHORIZATION' =&gt; 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo',
    'HTTP_USER_AGENT' =&gt; 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
    'HTTP_CONTENT_TYPE' =&gt; 'multipart/form-data',
    'HTTP_DNT' =&gt; '1',
    'HTTP_ACCEPT_ENCODING' =&gt; 'gzip, deflate',
    'HTTP_ACCEPT_LANGUAGE' =&gt; 'en-US,en;q=0.8,nl;q=0.6',
    'PHP_SELF' =&gt; '/index.php',
    'REQUEST_TIME_FLOAT' =&gt; (float) 1457090949.1208,
    'REQUEST_TIME' =&gt; (int) 1457090949,
    'HTTP_X_HTTP_METHOD_OVERRIDE' =&gt; null,
    'ORIGINAL_REQUEST_METHOD' =&gt; 'POST',
    'HTTPS' =&gt; false,
    'HTTP_X_REQUESTED_WITH' =&gt; null
]
[protected] _detectors =&gt; [
    'get' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'GET'
    ],
    'post' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'POST'
    ],
    'put' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'PUT'
    ],
    'patch' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'PATCH'
    ],
    'delete' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'DELETE'
    ],
    'head' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'HEAD'
    ],
    'options' =&gt; [
        'env' =&gt; 'REQUEST_METHOD',
        'value' =&gt; 'OPTIONS'
    ],
    'ssl' =&gt; [
        'env' =&gt; 'HTTPS',
        'options' =&gt; [
            (int) 0 =&gt; (int) 1,
            (int) 1 =&gt; 'on'
        ]
    ],
    'ajax' =&gt; [
        'env' =&gt; 'HTTP_X_REQUESTED_WITH',
        'value' =&gt; 'XMLHttpRequest'
    ],
    'flash' =&gt; [
        'env' =&gt; 'HTTP_USER_AGENT',
        'pattern' =&gt; '/^(Shockwave|Adobe) Flash/'
    ],
    'requested' =&gt; [
        'param' =&gt; 'requested',
        'value' =&gt; (int) 1
    ],
    'json' =&gt; object(Closure) {

    },
    'xml' =&gt; object(Closure) {

    },
    'mobile' =&gt; object(Closure) {

    },
    'tablet' =&gt; object(Closure) {

    },
    'api' =&gt; object(Closure) {

    }
]
[protected] _detectorCache =&gt; [
    'json' =&gt; false,
    'xml' =&gt; false,
    'api' =&gt; false,
    'post' =&gt; true,
    'ajax' =&gt; false
]
[protected] _input =&gt; '------WebKitFormBoundaryRDGaZEhAuN4lILOR
Content-Disposition: form-data; name="album_id"

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

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

  true
  ------WebKitFormBoundaryRDGaZEhAuN4lILOR--
  '
   [protected] _session =&gt; object(Cake\Network\Session) {
      [protected] _engine =&gt; null
      [protected] _started =&gt; null
      [protected] _lifetime =&gt; '1440'
      [protected] _isCLI =&gt; false
   }
 }
</code></pre>

<p>Does anyone know what I'm doing wrong here? It seems like I'm receiving the text values but not the file that is also send. Besides that, I have still no idea how to obtain the data like the album_id and description.</p>

<p><strong>edit 2</strong>
When I output $this->request->input() (without json_decode) with debug() I get the following output:</p>

<pre><code>'------WebKitFormBoundarykeFhH2KM4LdYgsH0
Content-Disposition: form-data; name="album_id"

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

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

true
------WebKitFormBoundarykeFhH2KM4LdYgsH0--
'
</code></pre>

<p>My question is now how do I obtain the file that also send through the same request? (the last boundary seems empty)</p>