0

I am very new in slim framework and i am using slim V3 i have done post route and it works fine but when i try to update record with put method it will works with Content-type = application/x-www-form-urlencoded and update my record with success

enter image description here

when i try to send file into slim api with POSTMAN Chrome Extension it will not sending file with form data request.

enter image description here

Here is my code

$app->put('/message/{message_id}', function ($request, $response, $args)
{
    $imagePath = '';

    $data = $request->getParsedBody();

    $files = $request->getUploadedFiles();

    $file = $files['file'];
    if ($file->getError() == UPLOAD_ERR_OK ) {
        $filename = $file->getClientFilename();
        $file->moveTo('assets/images/'.$filename);
        $imagePath = 'assets/images/'.$filename;
    }

    $message = Message::find($args['message_id']);
    $message->body = $data['message'];
    $message->user_id = $data['user_id'];
    $message->image_url = $imagePath;
    $message->save();

    if ($message->id) {
        return $response->withStatus(200)->withJson([
                'message_id' => $message->id,
                'message_uri' => '/message/'.$message->id,
            ]);
    }else{
        return $response->withStatus(400)->withJson(['message'=>'something went wrong!']);
    }

});

1 Answers1

1

When you want to upload a file with postman you need to remove or disable the Content-Type inside the header.

jmattheis
  • 10,494
  • 11
  • 46
  • 58
  • yes you are right i have to remove content-type from header but it works only `$app->post()` not working with `$app->put()` – Humaira Naz Nov 12 '16 at 19:03
  • @HumairaNaz have a look at [this question, why files are not populated by put requests](http://stackoverflow.com/questions/9464935/php-multipart-form-data-put-request) – jmattheis Nov 14 '16 at 11:48