cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: File Upload POST using libcurl

From: Dan Fandrich <dan_at_coneharvesters.com>
Date: Sat, 8 Nov 2008 21:19:26 -0800

On Sat, Nov 08, 2008 at 05:59:21PM +0500, koderr wrote:
> How I can do this:
>
> curl -F upload=@localfilename -F press=OK [URL]
>
> using libcurl?

The --libcurl option is one way to find out.

> Tried the following options:
>
> curl_easy_setopt(curl_handle, CURLOPT_URL, szUrl);
> curl_easy_setopt(curl_handle, CURLOPT_REFERER, szUrl);
> curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, TRUE);
> curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, TRUE);
> curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, szPostData);

From the manual for CURLOPT_POSTFIELDS: This POST is a normal
application/x-www-form-urlencoded kind...To make multipart/formdata
posts check out the CURLOPT_HTTPPOST option.

> curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, TRUE);

From the manual for CURLOPT_UPLOAD: "If the protocol is HTTP, uploading
means using the PUT request." Don't use this if you want a POST.

> curl_easy_setopt(curl_handle, CURLOPT_POST, TRUE);
> curl_easy_setopt(curl_handle, CURLOPT_READDATA, fd);
> curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, read_callback);
> curl_easy_setopt(curl_handle, CURLOPT_COOKIE, szCookie);
> curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, szAgent);
>
> It does PUT with "Expect: 100-continue".

Use curl_formadd to create the right post data and CURLOPT_HTTPPOST to
specify it. Take a look at the postit2.c example program to start.

>>> Dan

-- 
http://www.MoveAnnouncer.com              The web change of address service
          Let webmasters know that your web site has moved
Received on 2008-11-09