I'm trying to upload a file to my Slim Framework server using PUT. I've successfully created a POST route where I can retrieve the uploaded file with no problem, but if I send the same request to the same route, with the only difference being the method, I receive a file from POST, null from PUT.
Here's my router:
$app->put('/users/{id}', 'RideTimeServer\API\Controllers\UserController:update');
$app->post('/users/{id}', 'RideTimeServer\API\Controllers\UserController:update'); // DEBUG:
And then the request is just passed through controller to processPicture
method:
protected function processPicture(Request $request): string
{
// First look for an uploaded picture
if (!empty($request->getUploadedFiles()['picture'])) {
// http://www.slimframework.com/docs/v3/cookbook/uploading-files.html
$uploadedFile = $request->getUploadedFiles()['picture'];
/**
* { file, name, type }
*/
var_dump($uploadedFile);
// Then check URL
} elseif (!empty($request->getParsedBody()['picture'])) {
$url = $request->getParsedBody()['picture'];
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new UserException('Invalid picture URL', 400);
}
var_dump($url);
} else {
// $this->container['logger']->addInfo('Submitted user with no picture');
var_dump('Submitted user with no picture');
var_dump($request->getUploadedFiles());
var_dump($request->getParsedBody());
}
return '';
}
Now if I send
curl -X PUT \
http://localhost/ridetime/api/users/1 \
-H 'Authorization: Bearer t0k3n' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'picture=@/home/asdf/Tmp/Screenshot_20190125_005049.png'
i receive Submitted user with no picture
, []
and null
from the var_dump
, yet if I change the request to POST:
curl -X POST \
http://localhost/ridetime/api/users/1 \
-H 'Authorization: Bearer t0k3n' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'picture=@/home/asdf/Tmp/Screenshot_20190125_005049.png'
I get details of the uploaded file.
I tried replacing the -F 'picture=@...png
with --upload-file ./file.png
and the result was the same.