We're Hiring!

Mattermost, Inc.

File is not uploading and does not giving any error

Hey there!

I’m trying to upload a file using the rest API in which I’m using PHP-cURL and I’m unable to get any response from the server(success/fail), below is the code with the request I’m sending in PHP.

$curl = curl_init();
$channelID = $this->session->userdata(‘channelID’);
curl_setopt_array($curl, array(
CURLOPT_URL => “http://...*/api/v4/files?channel_id=”.$channelID,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => array(‘files’=> curl_file_create($image),‘channel_id’=>$channelID),
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$this->session->userdata(“token”),
“Content-Type: multipart/form-data”
),
));

	$response = curl_exec($curl);

where the $image = path of the selected file from the system.
After hitting this I’m not getting anything in the response from the server, I’m stuck in this pls help me out asap.

First, check the server logs (if you’re not a sysadmin, you’ll need them to allow it or check it for you). This is done in System Console > Server Logs. See if the Mattermost server is receiving your request and having an issue, or perhaps there’s another variable at play such as firewall issues. Reply back with the logs pertaining to the file upload if you still need help and we can take a look!

Could you also check that your Mattermost instance has the sufficient permissions to write files to the folder location that data is written to, and that you have enough storage?

I also suggest looking at your php.ini configuration file and making sure that the maximum allowable file upload size is set to a large enough value that you aren’t being prevented from uploading files.

If I’m uploading it through Postman it works but when using through curl it just gives a blank response in return, can you give me a solution for the blank response.

Thanks

So is the issue curl not uploading the file, or does curl upload the file but provide no response on completion?

Yes exactly it is the case the curl is not uploading the file and in response, it gives 200 but with the blank response.

So, I’m not sure about the blank response, as it should provide a response, at least it does with Go and curl requests. I was able to use my own Mattermost instance and curl to upload images successfully using the sample curl commands provided on the API documentation page, and it does upload to the server. The important thing to note, however, is that simply uploading to the server does not post the image, it just puts it into storage on the system. In order to actually post the image, you would need to retrieve the image ID from the system and use a webhook to post a message containing the image as an attachment identified either by the image URL to a static location (such as a raw GitHub URL) or the image ID from uploading it to the server. Does this help you at all?

Hello again @abhishek7504!
I was reading through the Mattermost documentation on the usage of the API and webhooks for uploading images and the one thing that I noticed is that it does state that the upload API feature is not to actually post a file but to put the file on the server to then be added to a future post by another user.
However, I did, in the course of my research, determine that the following script may be able to solve this issue, by allowing you to directly call the file from its location in the channel folder it was uploaded to, to be posted.

SERVER_URL = "http://chat.example.com/"
TEAM_ID = "team_id_goes_here"
CHANNEL_ID = "channel_id_goes_here"
USER_EMAIL = "you@example.com"
USER_PASS = "password123"
FILE_PATH = '/home/user/thing_to_upload.png'

import requests, json, os

# Login
s = requests.Session() # So that the auth cookie gets saved.
s.headers.update({"X-Requested-With": "XMLHttpRequest"}) # To stop Mattermost rejecting our requests as CSRF.

l = s.post(SERVER_URL + 'api/v3/users/login', data = json.dumps({'login_id': USER_EMAIL, 'password': USER_PASS}))

USER_ID = l.json()["id"]

# Upload the File.
form_data = {
        "channel_id": ('', CHANNEL_ID),
        "client_ids": ('', "id_for_the_file"),
        "files": (os.path.basename(FILE_PATH), open(FILE_PATH, 'rb')),
}
r = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/files/upload', files=form_data)

FILE_ID = r.json()["file_infos"][0]["id"]

# Create a post and attach the uploaded file to it.
p = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/channels/' + CHANNEL_ID + '/posts/create', data = json.dumps({
    'user_id': USER_ID,
    'channel_id': CHANNEL_ID,
    'message': 'Post message goes here',
    'file_ids': [FILE_ID,],
    'create_at': 0,
    'pending_post_id': 'randomstuffogeshere',
}))

I haven’t personally verified yet that this script works, but from my analysis of this code, it should work. Let me know if you have any further issues, and I’ll be happy to help further!