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 07:33:19 +0200

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.

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
Received on 2012-07-13