mromeo
asked on
How do you upload a file using Curl and HTTP using C++ on Linux?
I want to upload an xml file, using HTTP and Curl and C++. I am using libcurl on linux. I have no trouble downloading using curl, but uploading always gives me a problem. I get a segmentation fault whenever I call curl_easy_perform. Can somebody please post some sample code on how to upload a file to an http server. Thanks!!
ASKER
No, I want to upload a file to a server. I'll wait for a response saying it got the file. Here is my code:
CURL *hCurl = NULL;
...
CURLcode cc;
if (!hCurl)
{
cc = curl_global_init(CURL_GLOB AL_ALL);
hCurl = curl_easy_init();
}
if (hCurl)
{
FILE *fp = fopen("mbrTest.xml", "r");
char error_buffer[1024];
if (fp)
{
char *url = "http://1.0.0.2/cgi-bin/r50.cgi/ols-mbr.p";
cc = curl_easy_setopt(hCurl, CURLOPT_URL, url);
cc = curl_easy_setopt(hCurl, CURLOPT_UPLOAD, 1);
cc = curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1);
cc = curl_easy_setopt(hCurl, CURLOPT_INFILE, "mbrTest.xml");
cc = curl_easy_setopt(hCurl, CURLOPT_INFILESIZE, 104);
cc = curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, &error_buffer);
cc = curl_easy_perform(hCurl);
printf("curl easy perform completed -- CURL code = %d\n", cc);
fclose(fp);
}
curl_easy_cleanup(hCurl);
curl_global_cleanup();
}
CURL *hCurl = NULL;
...
CURLcode cc;
if (!hCurl)
{
cc = curl_global_init(CURL_GLOB
hCurl = curl_easy_init();
}
if (hCurl)
{
FILE *fp = fopen("mbrTest.xml", "r");
char error_buffer[1024];
if (fp)
{
char *url = "http://1.0.0.2/cgi-bin/r50.cgi/ols-mbr.p";
cc = curl_easy_setopt(hCurl, CURLOPT_URL, url);
cc = curl_easy_setopt(hCurl, CURLOPT_UPLOAD, 1);
cc = curl_easy_setopt(hCurl, CURLOPT_VERBOSE, 1);
cc = curl_easy_setopt(hCurl, CURLOPT_INFILE, "mbrTest.xml");
cc = curl_easy_setopt(hCurl, CURLOPT_INFILESIZE, 104);
cc = curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, &error_buffer);
cc = curl_easy_perform(hCurl);
printf("curl easy perform completed -- CURL code = %d\n", cc);
fclose(fp);
}
curl_easy_cleanup(hCurl);
curl_global_cleanup();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Both of those worked! Thanks!!
Can you show the coded that causes the segmentation fault ?