Uploading a file to slack using webwrite

I would like to be able to upload an image to slack using file.upload API and webwrite. The API ( https://api.slack.com/methods/files.upload ) requires to pass an argument "file" of 'multipart/form-data'.
I have tried this code and small variations unsuccessfully... any suggestion?
token='mytoken';
channel='mychannel';
filename='myimage.png';
fid=fopen(filename, 'rb');
data=fread(fid, Inf, '*char')';
fclose(fid);
options = weboptions;
options.RequestMethod = 'post';
options.MediaType = 'multipart/form-data';
options.HeaderFields = string({'token', token; 'channels', channel; 'filename', filename});
response = webwrite('https://slack.com/api/file.upload', data, options);
This yields 'invalid_form_data' and no image upload.
I also tried:
token='mytoken';
channel='mychannel';
filename='myimage.png';
fid=fopen(filename, 'rb');
data=fread(fid, Inf, '*char')';
fclose(fid);
options = weboptions;
options.RequestMethod = 'post';
options.MediaType = 'multipart/form-data';
response = webwrite(sprintf('https://slack.com/api/file.upload?token=%s&channels=%s&filename=%s&file=', token, channel, filename), data, options)
with the same results

답변 (1개)

Jan
Jan 2016년 11월 23일

0 개 추천

If you upload the file defining the file name, you do not have to provide the data separately. So try the secend method with providing the filename for "file=" also.

댓글 수: 3

Franco Stellari
Franco Stellari 2016년 11월 23일
Unfortunately that does not work. file= has to point to the content not the filename. In curl it would be -F file=@filename and not -F file=filename.
There is not way for the webwrite function to know that it has to send the actual data in the file to the web site.
in Python it would be a request where you pass file=open(filename,'rb')
Jan
Jan 2016년 11월 23일
Unfortuantely I find only CURL examples in the documentation. Because I've never worked with slack (or CURL) yet, I cannot guess the underlying patterns for the input arguments. But you can install CURL on your computer and use system or a C-Mex function to call it from Matlab. Then the examples from the slack pages run directly.
Juho
Juho 2020년 1월 22일
Have you found solution? Since Matlab 2018, there is content provider for files which could help you:https://se.mathworks.com/help/matlab/ref/matlab.net.http.io.fileprovider-class.html?searchHighlight=matlab.net.http.io.FileProvider&s_tid=doc_srchtitle
I have similar problem when trying to upload file to Jira from Matlab. https://se.mathworks.com/matlabcentral/answers/501255-adding-attachment-to-jira-with-matlab-restful-api

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

제품

질문:

2016년 11월 22일

댓글:

2020년 1월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by