user5448157
user5448157

Reputation:

How to send form data in slim framework v3 in PUT routing

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!']);
    }

});

Upvotes: 0

Views: 2010

Answers (1)

jmattheis
jmattheis

Reputation: 11125

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

Upvotes: 1

Related Questions