Using libcurl to create a valid POST

Posted by Haraldo on Stack Overflow See other posts from Stack Overflow or by Haraldo
Published on 2010-04-21T00:22:53Z Indexed on 2010/04/21 0:33 UTC
Read the original article Hit count: 343

Filed under:
|
|
static int get( const char * cURL, const char * cParam ) {

    CURL *handle;
    CURLcode result;
    std::string buffer;
    char errorBuffer[CURL_ERROR_SIZE];
    //struct curl_slist *headers = NULL;

    //headers = curl_slist_append(headers, "Content-Type: Multipart/Related");
    //headers = curl_slist_append(headers, "type: text/xml");       

    // Create our curl handle
    handle = curl_easy_init();

    if( handle ) {
        curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errorBuffer);
        //curl_easy_setopt(handle, CURLOPT_HEADER, 0);
        //curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(handle, CURLOPT_POST, 1);
        curl_easy_setopt(handle, CURLOPT_POSTFIELDS, cParam); 
        curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, strlen(cParam));
        curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, Request::writer);   
        curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buffer);
        curl_easy_setopt(handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 
        curl_easy_setopt(handle, CURLOPT_URL, cURL);

        result = curl_easy_perform(handle);
        curl_easy_cleanup(handle);
    }

    if( result == CURLE_OK ) {
        return atoi( buffer.c_str() );
    }

    return 0;
}

Hi there, first of all I'm having trouble debugging this in visual studio express 2008 so I'm unsure what buffer.c_str() might actually be returning but I am outputting 1 or 0 to the web page being posted to. Therefore I'm expecting the buffer to be one or the other, however I seem to only be returning 0 or equivalent.

Does the code above look like it will return what I expect or should my variable types be different? The conversion using "atoi" may be an issue.

Any thought would be much appreciated.

© Stack Overflow or respective owner

Related posts about visual-studio-2008

Related posts about c++