3

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?

2 Answers 2

4

The problem is you perform a multipart/form-data POST (adapted from this sample) where the Parse API expects a regular HTTP POST, i.e 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, i.e:

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.

Sign up to request clarification or add additional context in comments.

Comments

0
 - 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.