API Upload File not uploading

Summary

I am sending files to upload, I get return code 200, but the return is empty.

Steps to reproduce

Version 5.7.0.
Laravel PHP curl upload a file via API request:

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => env('MATTERMOST_URL')."/api/v4/files?channel_id=".$channel_id."&filename=".$destinationPath.'/'.$name,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => false,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array('files' => $destinationPath.'/'.$name, 'channel_id' => $channel_id),
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Bearer ".$_COOKIE['MMAUTHTOKEN'],
                    "Content-Type: multipart/form-data"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

Expected behavior

Sending the file path (which has already been temporarily saved in Laravel storage) and the channel through cURL, should receive the API return as follows:
{
“file_infos”: [
{
“id”: “string”,
“user_id”: “string”,
“post_id”: “string”,
“create_at”: 0,
“update_at”: 0,
“delete_at”: 0,
“name”: “string”,
“extension”: “string”,
“size”: 0,
“mime_type”: “string”,
“width”: 0,
“height”: 0,
“has_preview_image”: true
}
],
“client_ids”: [
“string”
]
}

Observed behavior

Receiving return with empty fields
{“file_infos”: [], “client_ids”: []}

Hi @paulus,

Wondering if this earlier thread might help: [SOLVED] Uploading an image with api v. 4 not working. If not, we’ll continue to troubleshoot.

Hello, amy

Thanks for the help.
I had seen this topic before, but it did not help me. There is an error situation there. I do not get error (like status code 403 or similar). My request returns successfully, but without the submitted file information.

Hi @paulus,

From a community member:

Would you be able to provide a curl command line to reproduce the issue? How did you confirm that the file you want to upload is available? (More a FYI: pr-mattermost-server-9835 )

Hi @amy.blais

I’m using cURL via PHP, but using just curl, the command is that:

curl -F 'files=/var/www/html/public/uploads/chat/p7nxd4pe8fgn9yyzrrujwuadww_user_er.PNG' \
-F 'channel_id=p7nxd4pe8fgn9yyzrrujwuadww' \
--header 'authorization: Bearer yiiw7i3r13nodm9tke1exppgne'
http://192.168.240.59:8065/api/v4/files 

The file exists in the directory and has permissions to read and execute (plus write for file owner).

Thanks

Hi @paulus,

Our developers said this sounds like a bug so I opened a bug ticket: https://mattermost.atlassian.net/browse/MM-14499.

1 Like

Thanks so much, @amy.blais

Hi everyone.
Does this problem / bug still persist ?.
I’m trying to upload a file but I have that same problem. The request is sent well but returns an empty result.

Hi @NexusNine, the bug ticket linked above is still open and under investigation by our devs.

Hi @NexusNine

For me, the uploads is now working well. Are using the same version that was using (5.7.0)?
I’m usign the lastest version right now and works correctly.

I installed follwing this guide: https://docs.mattermost.com/install/prod-docker.html

Are you using PHP? If you are, so this is my script. Maybe can help you:

            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => env('MATTERMOST_URL')."/api/v4/files",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => false,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array('files' => curl_file_create($destinationPath.'/'.$name), 'channel_id' => $channel_id),
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Bearer ".Auth()->user()->chat_token,
                    "Content-Type: multipart/form-data"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);
            curl_close($curl);