cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Post using https -- how can I avoid "Content-Length: 0"?

From: svante karlsson <saka_at_csi.se>
Date: Wed, 14 Mar 2012 12:18:15 +0100

>
>     ...it uses HTTP without any SOAP (Simple Object Access Protocol)
> wrappers.
>     Requests are sent to the service by sending a specially constructed XML
> documents using HTTP
>     POST. Responses are returned from the server in a similar way by
> sending an XML document
>     containing the results.

I attached a C++ snipped that does more or less the same thing.

bool http_post(const std::string& url, std::istream* istream, size_t
content_lenght, std::ostream* ostream)

use std::strstream or std::fstream for in and out data

/svante

static size_t write_callback_std_stream(void *ptr, size_t size, size_t
nmemb, std::ostream* stream)
    {
        assert(stream);
        size_t bytes = size*nmemb;
        stream->write((const char*) ptr, bytes);
        if (!stream->good())
            return 0;
        return bytes;
    }

    static size_t read_callback_std_stream(void *ptr, size_t size,
size_t nmemb, std::istream* stream)
    {
        assert(stream);
        stream->read((char*) ptr, size*nmemb);
        size_t bytes = (size_t) stream->gcount();
        return bytes;
    }

    static curl_socket_t open_callback(void* clientp, curlsocktype
purpose, curl_sockaddr* address)
    {
        std::cerr << "open " << inet_ntop(address->family,
&address->addr) << std::endl;
        return socket(address->family, address->socktype, address->protocol);
    }
        
    bool http_post(const std::string& url, std::istream* istream,
size_t content_lenght, std::ostream* ostream)
    {
        CURL* _curl = curl_easy_init();

       if (!_curl)
          return false;

        //curl_easy_setopt(_curl, CURLOPT_VERBOSE, 1L);

        curl_easy_setopt (_curl, CURLOPT_URL,url.c_str());
        curl_easy_setopt (_curl, CURLOPT_POST, 1);

        /* the transmitted content */
        curl_easy_setopt (_curl, CURLOPT_POSTFIELDSIZE_LARGE ,
(curl_off_t) content_lenght);
        /* we want to use our own read function */
        curl_easy_setopt(_curl, CURLOPT_READFUNCTION, read_callback_std_stream);
        /* now specify which file to upload */
        curl_easy_setopt(_curl, CURLOPT_READDATA, istream);

        curl_easy_setopt(_curl, CURLOPT_OPENSOCKETFUNCTION, open_callback);
        curl_easy_setopt(_curl, CURLOPT_OPENSOCKETDATA, NULL);

        /* the reply */
        curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION,
write_callback_std_stream);
        curl_easy_setopt(_curl, CURLOPT_WRITEDATA, ostream);

        struct curl_slist *headerlist=NULL;
        static const char buf[] = "Expect:";
        /* initalize custom header list (stating that Expect:
100-continue is not wanted */
        headerlist = curl_slist_append(headerlist, buf);
        curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, headerlist);

        CURLcode res = curl_easy_perform(_curl);
        curl_easy_cleanup(_curl);
        curl_slist_free_all(headerlist); // relase header list

        if (res == CURLE_OK)
            return true;

        //log_debug(url, curlcode_to_string(res));
        return false;
        }

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