Trying to build a C++ application to upload images to mattermost server(version 5.14.0).And I have get access token,channel id by using my C++ code.But I can't upload image by using libcurl. This is my CPP code(using libcurl right now):
void uploadPicture(string url,string channelId, string path,string fileName,string token)
{
url += "/api/v4/files";
CURL *curl;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "files",
CURLFORM_FILE, path.c_str(),
CURLFORM_FILENAME,"test.jpg",
CURLFORM_CONTENTTYPE, "image/jpeg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "client_ids",
CURLFORM_COPYCONTENTS, "test.jpg",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "channel_id",
CURLFORM_COPYCONTENTS, channelId.c_str(),
CURLFORM_END);
struct curl_slist *headers = NULL;
string auth = "Authorization:Bearer ";
auth += token;
headers = curl_slist_append(headers, "Content-Type:multipart/form-data");
headers = curl_slist_append(headers, auth.c_str());
CURLcode res;
curl = curl_easy_init();
if (curl)
{
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_setopt(curl, CURLOPT_HTTPPOST,formpost);
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getFileInfo);
res = curl_easy_setopt(curl, CURLOPT_POST, 1);
res = curl_easy_perform(curl);
if (res != 0)
{
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
}
}
But it won't work...Please tell me where the problem is.