Solved

How do you upload a file using Curl and HTTP using C++ on Linux?

Posted on 2007-04-05
5
2,414 Views
Last Modified: 2012-08-14
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!!
0
Comment
Question by:mromeo
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18858014
What do you mean by uploading ? Do you mean POST the XML request to an HTTP server, and wait for the XML response ?

Can you show the coded that causes the segmentation fault ?
0
 

Author Comment

by:mromeo
ID: 18858217
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_GLOBAL_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();
   }
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18858322
Did you take a look at this example (HTTP PUT) :

        http://curl.haxx.se/lxr/source/docs/examples/httpput.c
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 18858375
More precisely, try using :

      curl_easy_setopt(curl, CURLOPT_READDATA, fp);

instead of :

       cc = curl_easy_setopt(hCurl, CURLOPT_INFILE, "mbrTest.xml");



Also use :

       curl_easy_setopt(curl, CURLOPT_PUT, TRUE);

to choose HTTP PUT
0
 

Author Comment

by:mromeo
ID: 18858766
Both of those worked! Thanks!!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 75
Easy responsive table out of existing table 28 55
How can I increase the size of this logo 5 34
Hide cell in a table 2 9
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question