curl-library
Re: Uploading a file via HTTP Post
Date: Sun, 1 Jul 2007 08:37:03 -0400
Emil (and others),
Unfortunately your suggestions didn't work out. Perhaps there's
another way for me to show what it is that I need. I ran a Wireshark
capture so that I could capture the exact HTTP traffic that I need to
mimic with libcurl. Your suggestion got me very close...
What I need the post to look like:
Content-Disposition: form-data; name="upload_file";
filename="filename.torrent"
Content-Type: application/octet-stream
<Binary data follows>
What your suggestion got me:
Content-Disposition: form-data; name="upload_file"
<Binary data follows>
So clearly I'm missing two things:
1. the filename="filename.torrent" part of the form data.
2. the content-type for the binary data (this I know how to fix).
So I've been struggling with figuring out how to add this additional
filename field in using the form_add() function. It doesn't seem to
provide a way to do this...
Thanks again for all of your help.
Regards,
Carter
On Jul 1, 2007, at 8:27 AM, Carter R. Harrison wrote:
> Hi Carter,
>
> 2007/6/30, Carter R. Harrison <carterharrison_at_mac.com>:
>
>>
>> I've been struggling with this for a while now and I just can't
>> seem to
>> make this work... I want to upload a file (in this case a .torrent
>> file) to
>> a php web application. I can upload it via CURL on the command
>> line by
>> typing the following:
>> curl -F upload_file=@filename.torrent http://localhost/index.php
>>
>> now I'm trying to translate that line into the appropriate calls to
>> libcurl. Here's what I have.. The result is that CURL makes the
>> post, but
>> my file is never uploaded. Thanks in advance for any help.
>>
>> //Get a CURL.
>> CURL *mCURL;
>> mCURL = curl_easy_init();
>>
>> //Setup CURL to upload the torrent.
>> struct curl_httppost *post=NULL;
>> struct curl_httppost *last=NULL;
>> char *filename="filename.torrent";
>> curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file",
>> CURLFORM_COPYCONTENTS, filename, CURLFORM_END);
>>
>>
>
> To set the "upload_file" field to the actual content of the file "
> filename.torrent", I believe you are supposed to use
> CURLFORM_FILECONTENT
> instead of CURLFORM_COPYCONTENTS, replace the above line with:
> curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file",
> CURLFORM_FILECONTENT, filename, CURLFORM_END);
>
> Now I'm not sure how your server-side upload script works, and if
> the above
> mentioned does not work, you should try the following:
> curl_formadd(&post, &last, CURLFORM_COPYNAME, "upload_file",
> CURLFORM_FILE,
> filename, CURLFORM_END);
>
> Also, in this case since you are using curl_easy_perform() which
> doesn't
> return until it's done, you may use CURLFORM_PTRNAME instead. On large
> buffers this would avoid having multiple copies in memory, thus
> theoretically speed up your application (however, in practice you are
> probably restricted by network speed anyway).
>
> curl_easy_setopt(mCURL, CURLOPT_POSTFIELDS, post);
>
>> curl_easy_setopt(mCURL, CURLOPT_POST, 1);
>> curl_easy_setopt(mCURL, CURLOPT_URL, "http://localhost/index.php");
>>
>> //Post away
>> curl_easy_perform(mCURL);
>>
>>
>>
>>
Received on 2007-07-01