curl / Mailing Lists / curl-users / Single Mail

curl-users

Upload image to server Libcurl c++

From: Avi <avi.pars_at_gmail.com>
Date: Thu, 16 Feb 2017 09:33:17 +0000

I want to upload an image from a user's computer to my server.
I used an app called postman to help me, I successfully got results from
postman, it generates code for you, I got it to work with CMT curl:
curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token:
fb42cc8e-63dc-a079-9889-35088dd7f637" -H "Content-Type:
multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F
"imagefile=@duck.jpg" "http://0.0.0.0:5000/testJsonclassify_upload"

and it also generated C (LibCurl),

CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "
http://0.0.0.0:5000/testJsonclassify_upload");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "postman-token:
3610741e-ea9b-5825-855e-c1c40edf26c5");
headers = curl_slist_append(headers, "cache-control: no-cache");
headers = curl_slist_append(headers, "content-type: multipart/form-data;
boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS,
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"imagefile\"; filename=\"duck.jpg\"\r\nContent-Type:
image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");

CURLcode ret = curl_easy_perform(hnd);

So, I made some edits and put it in a C++ file.

That looks like this:
#include <iostream>
#include <sstream>
#include <curl/curl.h>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int writer(char *data, size_t size, size_t nmemb, string *buffer){
   int result = 0;
   if(buffer != NULL) {
      buffer -> append(data, size * nmemb);
      result = size * nmemb;
   }
return result;
}

int main(void)
{
    CURL *hnd = curl_easy_init();

  if(hnd)
  {

    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_URL, "
http://0.0.0.0:5000/testJsonclassify_upload");

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "postman-token:
99acbcae-6a84-d8b2-4035-c40c32a44825");
    headers = curl_slist_append(headers, "cache-control: no-cache");
    headers = curl_slist_append(headers, "content-type:
multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(hnd, CURLOPT_POSTFIELDS,
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data;
name=\"imagefile\"; filename=\"cat.jpg\"\r\nContent-Type:
image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");

    CURLcode ret = curl_easy_perform(hnd);
long http_code = 0;
curl_easy_getinfo (hnd, CURLINFO_RESPONSE_CODE, &http_code);

    if (ret == CURLE_OK)
{
cout << "Request sent" << endl;
if (http_code == 200)
    {
cout << "File sent and processed" << endl;
}
      else
      {
      cout << "file upload failed, check request" << endl;
    /* always cleanup */
    curl_easy_cleanup(hnd);

  }
  return 0;
  }
}
}

I ran a test and got it working (halfway) ..
Information is passed through the server, the image doesn't make it
through, but the name does.

I know this because these get saved in a tmp folder. The file name was
there, but I couldn't open the image, meaning it is corrupt or
non-existent.

Can you please help me narrow down this issue?

-----------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-users
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2017-02-16