cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: bad post strings?

From: Ryan Earl <heretic_at_mail.utexas.edu>
Date: Fri, 08 Jun 2001 21:41:49 -0500

At 08:23 PM 6/8/2001 -0500, John Baxter wrote:
>Per Ryan's suggestion I verified that the form is indeed a
>multipart/form data post.

It's just a newer standard that older servers and legacy code often don't support. Many server side processes only handle URL encoded data. It's not really a Curl problem.

Here are the relevant code bits from a test program I wrote with curl to do a URL encoded post. Sorry for the GUI code.

void CHttptestDlg::setDefaultCurlOpts( CURL *pCURL )
{
        // prepare headers once, reuse through rest of program
        const static char* headers[] = {
                "Accept: text/*",
                "Cache-Control: no-cache",
                "Content-Type: application/x-www-form-urlencoded",
        };
        const static int numHeaders = sizeof( headers ) / sizeof( *headers );
        
        static struct curl_slist *headerList = NULL;
        static int bHeaderlistInitialized = 0;

        if( !bHeaderlistInitialized ) {
                // add our headers
                for( int i = 0; i < numHeaders; ++i )
                        headerList = curl_slist_append( headerList, headers[ i ] );

                bHeaderlistInitialized = 1;
        }

        // setup standard options
        curl_easy_setopt( pCURL, CURLOPT_ERRORBUFFER, m_strCURLError );
        curl_easy_setopt( pCURL, CURLOPT_NOPROGRESS , 1 );
        curl_easy_setopt( pCURL, CURLOPT_HEADER , 1 );
        curl_easy_setopt( pCURL, CURLOPT_NOBODY , 0 );
        curl_easy_setopt( pCURL, CURLOPT_CLOSEPOLICY,
                                                         CURLCLOSEPOLICY_LEAST_RECENTLY_USED );
        curl_easy_setopt( pCURL, CURLOPT_USERAGENT,
                                                        "Mozilla/4.0 (compatible; autosubmitter 0.01a)" );
// curl_easy_setopt( pCURL, CURLOPT_WRITEFUNCTION, logOutput );
        curl_easy_setopt( pCURL, CURLOPT_FILE, m_logFile );
        curl_easy_setopt( pCURL, CURLOPT_HTTPHEADER, headerList );

        
// curl_easy_setopt( pCURL,
// curl_easy_setopt( pCURL,
// curl_easy_setopt( pCURL,
}

void CHttptestDlg::PostFromMap( CURL *pCurl, const map< string, string > &postMap ) {
        if( !postMap.size() )
                return;

        int i = 0;
        string postStr;

        for( std::map< string, string >::const_iterator it = postMap.begin();
                 it != postMap.end(); ++it, ++i ) {
                char *str;

                // seperator between different name:value pairs
                if( i ) postStr += "&";

                str = curl_escape( (char *)it->first.c_str(), it->first.length() );
                postStr += str;
                free( str );

                postStr += "=";
                str = curl_escape( (char *)it->second.c_str(), it->second.length() );
                postStr += str;
                free( str );
        }

        m_cEncodeDisplay = postStr.c_str();
        UpdateData( FALSE ); // display post strings to GUI

        curl_easy_setopt( pCurl, CURLOPT_POST, 1 );
        curl_easy_setopt( pCurl, CURLOPT_POSTFIELDS, postStr.c_str() );
        curl_easy_setopt( pCurl, CURLOPT_POSTFIELDSIZE, postStr.length() );
        UpdateError(); m_cProgress.StepIt();
        
        curl_easy_perform( pCurl );
        UpdateError(); m_cProgress.StepIt();
}

void CHttptestDlg::doConnection()
{
        CWaitCursor cursor; // setup waiting cursor

        m_pCURLConnection = curl_easy_init();
        m_cProgress.StepIt();

        setDefaultCurlOpts( m_pCURLConnection );
        m_cProgress.StepIt();

        int upper, lower;
        m_cProgress.GetRange( lower, upper );
        m_cProgress.SetPos( lower );

        CString cURL = m_strServerName + m_strObject;
        char *blah2 = new char[ cURL.GetLength() + 1 ];
        memcpy( blah2, cURL.GetBuffer( 1 ), cURL.GetLength() );
        blah2[ cURL.GetLength() ] = '\0';
        //char *pURL = new char[ cURL.GetLength() + 1 ];
        //memcpy( pURL, cURL.GetBuffer(), cURL.GetLength() + 1 );

        curl_easy_setopt( m_pCURLConnection, CURLOPT_URL, blah2 );
        UpdateError(); m_cProgress.StepIt();

        map< string, string > post;
        post[ "name" ] = "dope";
        post[ "verb" ] = "as";
        post[ "reason" ] = "me";

        PostFromMap( m_pCURLConnection, post );
        UpdateError(); m_cProgress.StepIt();

// m_cEncodeDisplay = strPostData;
// UpdateData( FALSE );

// curl_easy_perform( m_pCURLConnection );
// m_cProgress.StepIt();
        curl_easy_cleanup( m_pCURLConnection );
        m_cProgress.StepIt();

        delete [] blah2;

        //m_cUpdateReady.SetEvent();
}

_______________________________________________
Curl-library mailing list
Curl-library_at_lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/curl-library
Received on 2001-06-09