uploading file at https server fails using libcurl int main C++ - c++
I tried sample program to upload my file at https server using ssl certs.
It is failing.
I need your comment?
I cannot copy all code.
sample code:
// sample grep using curl_easy_setopt
curl_easy_setopt(l_curl, CURLOPT_HTTPHEADER, NULL);
curl_easy_setopt(TestLibCurPtr, CURLOPT_CAINFO, CAINFOptr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_CAINFO, CAINFOptr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_CAPATH, capathptr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_CAPATH, capathptr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_DEBUGFUNCTION, debugFunct);
curl_easy_setopt(TestLibCurPtr, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(TestLibCurPtr, CURLOPT_HTTPHEADER, headerptr);
curl_easy_setopt((*TestLibCurPtr), CURLOPT_HTTPHEADER, NULL);
curl_easy_setopt(TestLibCurPtr, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_NOSIGNAL,1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_POST, 1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_POSTFIELDS, postfieldptr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_POSTFIELDS, postfieldptr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLCERT, sslcertptr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLCERT, sslcertptr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLKEY, sslkeyptr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLKEY, sslkeyptr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION, 1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION, 2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION, 3);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_0);
curl_easy_setopt(TestLibCurPtr, CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_TIMEOUT, TimeoutVar1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_TIMEOUT, TimeoutVar2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_URL, OptrUrlPtr1);
curl_easy_setopt( TestLibCurPtr, CURLOPT_URL, OptrUrlPtr2 );
curl_easy_setopt(TestLibCurPtr, CURLOPT_URL, OptrUrlPtr2);*/
curl_easy_setopt(TestLibCurPtr, CURLOPT_USERPWD, SamplePwdPtr);
curl_easy_setopt(TestLibCurPtr, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(TestLibCurPtr, CURLOPT_WRITEDATA, BufferPtr1);
curl_easy_setopt(TestLibCurPtr, CURLOPT_WRITEDATA, BufferPtr2);
curl_easy_setopt(TestLibCurPtr, CURLOPT_WRITEFUNCTION, WriteFunctionArg);
sample using
curlRet = curl_easy_perform(frameworkVar);
initRet = curl_easy_init();
curl_formadd(formpostvar, lastptr, CURLFORM_COPYNAME, "file", CURLFORM_BUFFER, buffervar, CURLFORM_BUFFERPTR, bufferptrvar, CURLFORM_BUFFERLENGTH, bufferlenvar, CURLFORM_CONTENTTYPE, "plain/text", CURLFORM_END);
curl_formadd( formpostvar, lastptr, CURLFORM_COPYNAME, "type", CURLFORM_COPYCONTENTS, "sample.txt", CURLFORM_END);
curl_formadd( formpostvar, lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, "./sample.txt", CURLFORM_CONTENTTYPE, "plain/text", CURLFORM_END);
curl_formadd( formpostvar, lastptr, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, "sample.txt", CURLFORM_END);
curl_formadd( formpostvar, lastptr, CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, "sample.txt", CURLFORM_END);
curl_formadd( formpostvar, lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END);
curl_formadd(formpostvar, lastptr, CURLFORM_COPYNAME, "type", CURLFORM_COPYCONTENTS, contentvar, CURLFORM_END);
easy_perform returning OK result. However file not uploading at https url.
01)
Not related to proxy.
C++ libcurl FTP upload over proxy not working
Above code written after using grep command.
it is being used by multiple threads(mutex locking/unlocking) to do all actions at browser:
upload/delete/list/sort...
hence grep displaying multiple times.
I tried sample code using network search:
#include <sys/wait.h>
#include <sys/signal.h>
#include <unistd.h>
#include <vector>
#include <curl/curl.h>
#include <sys/errno.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <curl/curl.h>
using namespace std;
#define MYUSER "MYUSER"
#define MYPASSWD "MYPASSWD"
#define MYURL "https://MYURL"
int main( int argc, char** argv )
{
CURL *curl = NULL;
CURLcode res = CURLE_OK ;
streampos size = 0;
char* memblock = NULL;
ifstream file ("stackove.txt", ios::in|ios::binary|ios::ate );
if( file.is_open() )
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg );
file.read( memblock, size );
file.close();
struct curl_httppost *formpost=NULL;
curl_global_init( CURL_GLOBAL_ALL );
CURL* p_curl = NULL;
p_curl = curl_easy_init();
if ( NULL == p_curl )
{
cout << "Error at initializing the libcurl handler: " << strerror( errno ) << "\n";
return -1;
}
curl_easy_setopt( p_curl, CURLOPT_FAILONERROR, 0);
curl_easy_setopt( p_curl, CURLOPT_CAINFO, "samplecacert.pem");
curl_easy_setopt( p_curl, CURLOPT_CAPATH, ".");
curl_easy_setopt( p_curl, CURLOPT_SSLCERT, "samplecert.pem");
curl_easy_setopt( p_curl, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt( p_curl, CURLOPT_SSLKEY, "clientSampleKey.pem");
curl_easy_setopt( p_curl, CURLOPT_SSH_PRIVATE_KEYFILE, "clientSampleKey.pem");
curl_easy_setopt( p_curl, CURLOPT_SSL_VERIFYPEER, 0 );
curl_easy_setopt( p_curl, CURLOPT_VERBOSE, 1 );
vector <string> header;
header.push_back("Content-Type: application/xml; charset=\"utf-8\"");
header.push_back("CIMOperation: MethodCall");
header.push_back("CIMMethod: sample_method");
curl_slist *headerArray = NULL;
for( int i=0; (int)header.size() > i; ++i )
{
headerArray = curl_slist_append(headerArray,header[i].c_str());
}
curl_easy_setopt( p_curl, CURLOPT_HTTPHEADER, headerArray );
curl_easy_setopt( p_curl, CURLOPT_NOSIGNAL, 1 );
curl_easy_setopt( p_curl, CURLOPT_SSL_VERIFYHOST, 0 );
curl_easy_setopt( p_curl, CURLOPT_USERNAME, MYUSER );
curl_easy_setopt( p_curl, CURLOPT_PASSWORD, MYPASSWD );
struct curl_httppost *lastptr=NULL;
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_BUFFER, "stackove.txt", CURLFORM_BUFFERPTR, memblock, CURLFORM_BUFFERLENGTH, size, CURLFORM_END );
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_PROXY, NULL);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);
curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "stackove.txt");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 579);
if( NULL != curl )
{
curl_easy_setopt( curl, CURLOPT_URL, MYURL );
curl_easy_setopt( curl, CURLOPT_HTTPPOST, formpost );
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "");
res = curl_easy_perform( curl );
if( CURLE_OK != res )
{
fprintf( stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror( res ) );
}
else
{
}
curl_easy_cleanup( curl );
curl = NULL;
curl_formfree( formpost );
}
curl_global_cleanup();
file.close();
}
return 0;
}
Current output:
$ ./a.out
HTTP/1.1 401 Unauthorized
Date: Thu, 09 Feb 2023 09:28:52 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 111
Connection: keep-alive
X-RateLimit-Limit-Second: 1000000
X-RateLimit-Remaining-Second: 999999
RateLimit-Remaining: 999999
RateLimit-Limit: 1000000
RateLimit-Reset: 1
WWW-Authenticate: Bearer realm="R6-Gateway"
X-Kong-Upstream-Latency: 4
X-Kong-Proxy-Latency: 1
Via: kong/2.3.3
Strict-Transport-Security: max-age=31536000; includeSubDomains
{
"logId" : "c2bcf9c1-11b7-4a4c-8f0d-1355887d6056",
"messages" : [ {
"message" : "Unauthorized"
} ]
My last comment on:
Feb 10 at 2:34
I will wait for any comments from good programmers.
Current time: Mon 13-Feb-2023 08:10 AM IST
Related
I cannot send an email using curl and c++
im trying to send an email with curl and c++ just like this example but when i execute the program: #include <iostream> #include <curl/curl.h> int main() { int a; char errbuf[CURL_ERROR_SIZE] = {0}; CURL *curl = curl_easy_init(); CURLcode res; struct upload_status upload_ctx = { 0 }; if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "smtps://smtp.gmail.com:465"); curl_easy_setopt(curl, CURLOPT_MAIL_FROM, ""); curl_easy_setopt(curl, CURLOPT_PASSWORD, ""); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, ""); curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_CAINFO, "D:\\MinGW\\cacert.pem"); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } check_errors(res, errbuf); return 0; } i get this error: curl_easy_perform() failed: Failed sending data to the peer libcurl: (55) MAIL failed: 530 I deleted most of the code because i couldn't post the question but the rest is in the example.
The problem i had in my code was that i thought that by using: curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "myemail#example.com"); i was specifyng the username, which was in fact, an error. To specify the username libcurl has another option: curl_easy_setopt(curl, CURLOPT_USERNAME, "myemail#example.com");
How to convert curl command with -F option to libcurl
I succeed to upload an image file to my blog by using curl command like this. curl -F 'access_token=xxx' -F 'blogName=xxx' -F 'uploadedfile=#xxx.png' https://www.tistory.com/apis/post/attach And... to use it on my c++ project, I converted it to libcurl like this. curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.tistory.com/apis/post/attach"); curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.61.0"); curl_mime *formpost = curl_mime_init(curl); curl_mimepart *part = nullptr; part = curl_mime_addpart(formpost); curl_mime_name(part, "access_token"); curl_mime_data(part, "xxx", CURL_ZERO_TERMINATED); part = curl_mime_addpart(formpost); curl_mime_name(part, "blogName"); curl_mime_data(part, "xxx", CURL_ZERO_TERMINATED); part = curl_mime_addpart(formpost); curl_mime_name(part, "uploadedfile"); curl_mime_filename(part, "xxx.png"); curl_mime_type(part, "image/png"); curl_easy_setopt(curl, CURLOPT_MIMEPOST, formpost); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); res = curl_easy_perform(curl); ... } This converted code doesn't work. And the result is response code 400. Actually I don't know what function of libcurl is correspond to -F option of curl command. Somebody let me know how to convert this curl command at the top to libcurl, or what function do I have to study about. Thanks in advance.
After running the following command (useful life hack here!): curl -F 'access_token=xxx' -F 'blogName=xxx' -F 'uploadedfile=#xxx.png' https://www.tistory.com/apis/post/attach --libcurl example.cc && cat example.cc I got the following code (tidied up a bit for legibility on SO): #include <curl/curl.h> int main(int argc, char *argv[]) { CURLcode ret; CURL *hnd; curl_mime *mime1; curl_mimepart *part1; mime1 = NULL; hnd = curl_easy_init(); curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L); curl_easy_setopt(hnd, CURLOPT_URL, "https://www.tistory.com/apis/post/attach"); curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L); mime1 = curl_mime_init(hnd); part1 = curl_mime_addpart(mime1); curl_mime_data(part1, "xxx", CURL_ZERO_TERMINATED); curl_mime_name(part1, "access_token"); part1 = curl_mime_addpart(mime1); curl_mime_data(part1, "xxx", CURL_ZERO_TERMINATED); curl_mime_name(part1, "blogName"); part1 = curl_mime_addpart(mime1); curl_mime_filedata(part1, "xxx.png"); curl_mime_name(part1, "uploadedfile"); curl_easy_setopt(hnd, CURLOPT_MIMEPOST, mime1); curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.60.0"); curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L); curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS); curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/home/MY_USER/.ssh/known_hosts"); curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L); ret = curl_easy_perform(hnd); curl_easy_cleanup(hnd); hnd = NULL; curl_mime_free(mime1); mime1 = NULL; return (int)ret; }
Uploading a picture to a Password Protected Directory using libcurl and C++
I'm trying to upload pictures using c++ application with libcurl to my server, the file in my server is a protected directory, I tried everything I know but it didn't work. struct curl_slist *headerlist = NULL; const char * Picture = "C:\xxxx"; FILE *fd = fopen(Picture, "rb"); struct stat file_info; fstat(fileno(fd), &file_info); headerlist = curl_slist_append(headerlist, "user : xxxx"); headerlist = curl_slist_append(headerlist, "password : xxxxx"); headerlist = curl_slist_append(headerlist, "Content-Type: image/jpeg"); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures"); 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); res = curl_easy_perform(curl); if (res != CURLE_OK) { MessageBox::Show("Picture Failed"); } curl_easy_cleanup(curl); curl_global_cleanup();
user and password are not HTTP headers, so you should not be passing them via CURLOPT_HTTPHEADER. Use CURLOPT_USERNAME and CURLOPT_PASSWORD instead. Also look at CURLOPT_HTTPAUTH. Try this: curl_global_init(CURL_GLOBAL_ALL); ... const char *Picture = "C:\\xxxx"; FILE *fd = fopen(Picture, "rb"); if (!fd) { MessageBox::Show("Cannot Open Picture File"); } else { struct stat file_info; fstat(fileno(fd), &file_info); curl = curl_easy_init(); if (!curl) { MessageBox::Show("Cannot Initialize CURL Session"); } else { struct curl_slist *headerlist = curl_slist_append(NULL, "Content-Type: image/jpeg"); curl_easy_setopt(curl, CURLOPT_URL, "http://xxxx.000webhostapp.com/Pictures"); curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxx"); curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxx"); 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); res = curl_easy_perform(curl); if (res != CURLE_OK) { MessageBox::Show("Picture Failed"); } curl_easy_cleanup(curl); } fclose(fd); } ... curl_global_cleanup();
HTTP POST with Basic Auth and Fields (libcurl c++)
I am trying to make an HTTP POST using libcurl. The HTTP POST needs to perform a Basic Authentication, include a field and send a file in multi part. What I am using at the moment is the following: FILE* fo = nullptr; std::string fullErrPath(errPath); fullErrPath.append("debug.txt"); fo = fopen(fullErrPath.c_str(), "wb"); CURL *curl; CURLcode res; struct curl_httppost* formpost = nullptr; struct curl_httppost* lastptr = nullptr; struct curl_slist* headerlist = nullptr; static const char buf[] = "Expect:"; const char* imgPath = "/path/to/image.jpg"; curl_global_init(CURL_GLOBAL_ALL); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "jpgdata", CURLFORM_FILE, imgPath, CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send" CURLFORM_END); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(curl, CURLOPT_URL, urlupload); headerlist = curl_slist_append(headerlist, buf); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "field=1"); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC); curl_easy_setopt(curl, CURLOPT_USERPWD, credentials); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl, CURLOPT_STDERR, fo); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // Response information int httpCode(0); static std::string httpData; curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); curl_formfree(formpost); curl_slist_free_all(headerlist); fclose(fo); } I left on purpose this part "//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "field=1");" commented because I found online that it does not make sense to use it with CURLOPT_HTTPPOST. This is the post. But code above does not work. First, the http response code I get from the server is 400 and by checking "debug.txt", in fact, there is no data. I see only the header. Second, I need to include the POST field "field=1", but I do not know how to do it without using the command "curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "field=1");". Thanks.
First, you want to set CURLOPT_USERPWD for the Basic auth. Then, you need to decide which format of the data you want to POST. Do you want it to be multipart formpost or "regular" - you cannot mix them as that's just how HTTP works. The curl_formadd() you're doing so far is multipart formpost, and CURLOPT_POSTFIELDS is the regular one ("application/x-www-form-urlencoded"). So you say you want to add a "field=1" to the post, and to do that and keep this a multipart formpost you can insert the following snippet after your second call to curl_formadd. It adds another "part" to the post with the name and contents you asked for: curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "field", CURLFORM_COPYCONTENTS, "1" CURLFORM_END);
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, 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.
- 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); }