tags:

views:

907

answers:

2

how to upload file by POST in libcurl?(c++)

+1  A: 

From the documentation here:

When using libcurl's "easy" interface you init your session and get a handle (often referred to as an "easy handle"), which you use as input to the easy interface functions you use. Use curl_easy_init to get the handle.

You continue by setting all the options you want in the upcoming transfer, the most important among them is the URL itself (you can't transfer anything without a specified URL as you may have figured out yourself). You might want to set some callbacks as well that will be called from the library when data is available etc. curl_easy_setopt is used for all this.

When all is setup, you tell libcurl to perform the transfer using curl_easy_perform. It will then do the entire operation and won't return until it is done (successfully or not).

After the transfer has been made, you can set new options and make another transfer, or if you're done, cleanup the session by calling curl_easy_cleanup. If you want persistent connections, you don't cleanup immediately, but instead run ahead and perform other transfers using the same easy handle.

So it looks like you need to call the following:

  1. curl_easy_init (initialize the curl session)
  2. curl_easy_setopt (setup the session options)
  3. curl_easy_perform (perform the curl)
  4. curl_easy_cleanup (delete the session)

Given that these are C APIs you should have no problem calling them within a C++ source file.

fbrereto
+2  A: 

Are you referring to RFC 1867 (i.e., what the browser sends when the user submits an HTML form containing an input field with type="file")?

If that's the case, you may be interested in http://curl.haxx.se/libcurl/c/postit2.html

Éric Malenfant