cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Is it possible to simply write to a socket?

From: Oscar Koeroo <okoeroo_at_nikhef.nl>
Date: Fri, 13 Jul 2012 11:06:44 +0200

Hi Igor,

My code snippet shows that I have my to-be posted data in a buffer. I
present libcurl with the length of it and when the code hits the
curl_easy_perform() it will POST the data. Libcurl will take automagically
take care of the splitting the data over mulitple HTTP messages as a
multi-part POST.

In my code snippet the hc->out is the output buffer to-be POST-ed.

Currently I might have made a code mistake myself by setting POST fields
data and the READ stuff. But bare with me that this is just an example. :-)

In your case I can imagine that you need to send a few HTTP POST's to get
the complete job done. This is all doable and there are example on how to
make this even more nice on the Curl website.

I think this information might be of assistance too:
http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTHTTPPOST

        Oscar

On 13-07-12 09:05, Igor Korot wrote:
> Hi, Oscar,
>
> On Thu, Jul 12, 2012 at 10:33 PM, Oscar Koeroo <okoeroo_at_nikhef.nl> wrote:
>> Hi Igor,
>>
>> Your approach breaks the fact that POST is typically a form-post. I recently
>> had a problem with that breakage when I tried to POST data in JSON as raw
>> data. libcurl can do this, but consider the incompatibility with something
>> like Django.
>
> The server code is written in ASP.
> The code has a form behind.
>
> The approach for the ASP code is as follows:
> 1. Send the boundary string.
> 2. Now post some arbitrary data about the file we are posting (like
> the name and id).
> 3. Tell server we are posting the file in the image/bitmap format.
> 4. Send the file by chunks
> 5. Send the same boundary string again to indicate the transfer is done.
>
> I didn't write the server code and I see only VB client code that I
> need to translate.
> Now in VB steps 2 and 3 are done in one write() call.
>
> It is a multiform POST as the file is read and send out by chunks.
> And so I have to follow a protocol to communicate with the server.
>
> Any help on libcurl implementation?
>
> It looks like I can upload the file with libcurl using the read
> function approach, but
> then I need to post it somehow.
>
> I guess I need to look at the asp code in order to upload and post, right?
>
> Thank you.
>
>>
>> Here's an example in C:
>>
>> [code]
>> static size_t
>> _curl_memread (void *ptr, size_t size, size_t nmemb, void *userp) {
>> size_t read_size = 0;
>> if (!userp) {
>> return 0;
>> }
>> read_size = evbuffer_copyout((struct evbuffer *)userp, ptr, size * nmemb);
>> return read_size;
>> }
>>
>> /* POST */
>> curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS,
>> (char *)evpull(hc->out));
>> curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE,
>> (long)evbuffer_get_length(hc->out));
>> curl_easy_setopt(curl_handle, CURLOPT_URL, hc->url_query);
>> curl_easy_setopt(curl_handle, CURLOPT_READFUNCTION, _curl_memread);
>> curl_easy_setopt(curl_handle, CURLOPT_READDATA, (void *)hc->out);
>>
>> curl_easy_perform(curl_handle);
>> [/code]
>>
>>
>> cheers,
>>
>> Oscar
>>
>>
>>
>> On 13-07-12 07:01, Igor Korot wrote:
>>> Guenter,
>>>
>>> On Thu, Jul 12, 2012 at 7:41 PM, Guenter <lists_at_gknw.net> wrote:
>>>> Hi Igor,
>>>> Am 13.07.2012 01:49, schrieb Igor Korot:
>>>>
>>>>> I'm transferring the code from VB to C/C++ to make it cross-platform.
>>>>> In VB, there is following code:
>>>>>
>>>>> [code]
>>>>> Dim boundary As String = "---------------------------"&
>>>>> DateTime.Now.Ticks.ToString("x")
>>>>> Dim newLine As String = System.Environment.NewLine
>>>>> Dim boundaryBytes As Byte() =
>>>>> System.Text.Encoding.ASCII.GetBytes(newLine& "--"& boundary&
>>>>> newLine)
>>>>> Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
>>>>>
>>>>> request.ContentType = "multipart/form-data; boundary="& boundary
>>>>> request.Method = "POST"
>>>>> request.KeepAlive = True
>>>>> request.Credentials = Net.CredentialCache.DefaultCredentials
>>>>>
>>>>> Using requestStream As IO.Stream = request.GetRequestStream()
>>>>> requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
>>>>> End Using
>>>>> [/code]
>>>>>
>>>>> AFAIU, it just simply writes boundary bytes to the socket as an ASCII
>>>>> bytes sequence.
>>>>> There is post-ing involved, no form submittal
>>>>> Is it possible to do the same with libcurl?
>>>>
>>>> although this might be possible with libcurl - if I were you I would use
>>>> Perl for this task; you have then a simple script which runs on almost every
>>>> platform, no hassle with IDEs, compilers and MS dotnet redistributables
>>>> install which you most likely need if you compile with MSVC 2010, and
>>>> finally easy to write with your favourite text editor ...
>>>
>>> If it was that easy... ;-)
>>> The program has GUI and it should be cross-platform, therefore I chose
>>> wxWidgets/C++.
>>> And I need the file to be transferred in addition to this simple
>>> string for processing
>>> and get the result of this processing back.
>>>
>>> Thank you.
>>>
>>>>
>>>> libcurl is more meant for high-level tasks which you dont want ...
>>>>
>>>> Gün.
>>>>
>>>>
>>>>
>>>> -------------------------------------------------------------------
>>>> List admin: http://cool.haxx.se/list/listinfo/curl-library
>>>> Etiquette: http://curl.haxx.se/mail/etiquette.html
>>>
>>> -------------------------------------------------------------------
>>> List admin: http://cool.haxx.se/list/listinfo/curl-library
>>> Etiquette: http://curl.haxx.se/mail/etiquette.html
>>>
>>
>>
>> -------------------------------------------------------------------
>> List admin: http://cool.haxx.se/list/listinfo/curl-library
>> Etiquette: http://curl.haxx.se/mail/etiquette.html
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-07-13