Mattermost, Inc.

[Solved] How to upload picture to mattermost server by using libcurl?

Trying to build a c++ app to upload picture to mattermost server.I am using libcurl to upload right now. I have tried many sample on the internet,but none of them is working…Can I get some code sample that will work?Thanks a lot.

At last I find a solution:
void uploadByMimeApi(string url, string channelId, string path, string fileName, string token)
url += “/api/v4/files”;

CURL *curl = curl_easy_init();
curl_mime *mime;
curl_mimepart *part;
curl_mimepart *part2;
curl_mimepart *part3;

/* Build an HTTP form with a single field named "data", */
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);

curl_mime_name(part, "files");
curl_mime_filedata(part, path.c_str());
curl_mime_type(part, "image/jpeg");

part2 = curl_mime_addpart(mime);
curl_mime_data(part2, channelId.c_str(), CURL_ZERO_TERMINATED);
curl_mime_name(part2, "channel_id");

part3 = curl_mime_addpart(mime);
curl_mime_data(part3, "test", CURL_ZERO_TERMINATED);
curl_mime_name(part3, "client_ids");

//header issue
struct curl_slist *headers = NULL;
static const char buf[] = "Expect:";
string auth = "Authorization:Bearer ";
auth += token;
headers = curl_slist_append(headers, auth.c_str());
headers = curl_slist_append(headers, buf);

/* Post and send it. */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getFileInfo);

/* Clean-up. */