简体   繁体   中英

uploading image to Parse server using libcurl in c++

I have the below code which performs the upload operation! its returning me, url and name but that url is invalid/there is no image, what i'm doing wrong?

CURL *curl;
CURLcode res;

struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:";

curl_formadd(&formpost,
             &lastptr,
             CURLFORM_COPYNAME, "sendfile",
             CURLFORM_FILE, "/Users/xxxx/Downloads/google.jpg",
             CURLFORM_END);

curl_formadd(&formpost,
             &lastptr,
             CURLFORM_COPYNAME, "filename",
             CURLFORM_COPYCONTENTS, "/Users/xxxx/Downloads/google.jpg",
             CURLFORM_END);


headerlist = curl_slist_append( headerlist, "X-Parse-Application-Id: xxxxxxx");
headerlist = curl_slist_append( headerlist, "X-Parse-REST-API-Key: xxxxxxxx");
headerlist = curl_slist_append( headerlist, "Content-Type: image/jpeg");
headerlist = curl_slist_append(headerlist, buf);

curl = curl_easy_init();
if(curl)
{
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
    curl_easy_setopt(curl, CURLOPT_URL, "https://api.parse.com/1/files/pic.jpg");
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);


res = curl_easy_perform(curl);

if(res != CURLE_OK){
    fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}

the output is,
{"url":" http://files.parsetfss.com/3603be25-6ce1-4ee5-ba97-0fad4406d6cc/tfss-c7432593- bd61-424b-8a84-41308045040d-pic.jpg","name":"tfss-c7432593-bd61-424b-8a84-41308045040d-pic.jpg"}

but url contains no image, do i need do change anything?

second question is how do i access these url and name in the code?

The problem is you perform a multipart/form-data POST (adapted from this sample ) where the Parse API expects a regular HTTP POST , ie simply post the binary data (= image file content) as body.

To solve your problem you can use CURLOPT_POST and provide the file content as described by the official file upload sample , ie:

FILE *fd = fopen("/path/to/image.jpg", "rb");
struct stat file_info;
fstat(fileno(fd), &file_info);

/* ... */

curl_easy_setopt(curl, CURLOPT_URL, "https://api.parse.com/1/files/pic.jpg");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (curl_off_t)file_info.st_size);

And keep the header list as is.

See CURLOPT_POST for alternatives on how to pass the binary data.

 - First you need to initilise curl * variable ;  put image on c drive

to upload; declare post * variable ; add post variable in curl_formadd

  if (curl)
{
    curl_formadd(&post, &last,
                 CURLFORM_COPYNAME, "image",
                 CURLFORM_FILE, "C://bubble.jpg",
                 CURLFORM_END);

     curl_formadd(&post, &last,
                 CURLFORM_COPYNAME, "image",
                 CURLFORM_COPYCONTENTS, "C://bubble.jpg",
                 CURLFORM_END);

    /////////////////////////////////////////////////////////////////////

    curl_formadd(&post, &last,
                 CURLFORM_COPYNAME, "key",
                 CURLFORM_COPYCONTENTS, "1748ee815be8f13cea057a29a7ec47ee",
                 CURLFORM_END);

    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost//nutircsupload//simpleupload.php");
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);

    res = curl_easy_perform(curl);
    if (res)
    {
        return 0;
    }

    curl_formfree(post);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM