curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Turning two command-line curl calls into c++, using libcurl

From: J via curl-library <curl-library_at_lists.haxx.se>
Date: Mon, 23 May 2022 11:15:21 -0400

Hi!

I'm trying to turn a (working) command-line call to curl into an equivalent
c++ program.

The following command line I was able to translate into c++

curl --location --request POST 'https://servername.domain/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=ie_client' \
--data-urlencode 'client_secret=*secret_code*' \
--data-urlencode 'grant_type=client_credentials' 2>/dev/null

Translated to c++, this became

bool getToken( std::string & token ) {

    CURL * curl;
    CURLcode res;

    std::string json_record;

    curl = curl_easy_init();

    if ( curl ) {

        curl_easy_setopt( curl, CURLOPT_URL, "
https://servername.domain/token" );
        curl_easy_setopt( curl, CURLOPT_HEADER, "Content-Type:
application/x-www-form-urlencoded" );
        curl_easy_setopt( curl, CURLOPT_POST, 1L );

        curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, token_callback );
        curl_easy_setopt( curl, CURLOPT_WRITEDATA, &json_record );

        curl_easy_setopt( curl, CURLOPT_POSTFIELDS,
"client_id=ie_client&client_secret=*secret_code*&grant_type=client_credentials"
);

        res = curl_easy_perform( curl );

        curl_easy_cleanup( curl );

        if( CURLE_OK != res ) {

            fprintf( stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ));
            return false;

        }

    }

    if ( ! json_record.empty() ) {

        return true;

    }

    return false;

}

The function retrieves data, from which I am able to extract the token.

But it is the second part, using the token that I haven't been able to
translate.

The (working) command-line for that is

curl --location --request POST
https://otherserver.domain/command/nodename?commandId=get-some-data\&mode=sync
--header "Authorization: Bearer *token_retrieved_by_previous_command_line*"

The command-line functions and retrieves the data, no problem.

However, the following code always returns an error message from the server

bool ExecuteCommand( const char * token ) {

    CURL * curl;
    CURLcode res;

    std::string json_record;

    curl = curl_easy_init();

    if ( curl ) {

        curl_easy_setopt( curl, CURLOPT_URL, "
https://otherserver.domain/command/nodename" );
        curl_easy_setopt( curl, CURLOPT_HEADER, ( (
(std::string)"Authorization: Bearer" ) + token ).c_str() );
        curl_easy_setopt( curl, CURLOPT_POST, 1L );
        curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1L );

        curl_easy_setopt( curl, CURLOPT_POSTFIELDS,
"commandId=get-some-data&mode=sync" );

        res = curl_easy_perform( curl );

        curl_easy_cleanup( curl );

        if( CURLE_OK != res ) {

            fprintf( stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror( res ));
            return false;

        }

    }

    return true;

}

Although the curl_easy_perform call does succeed, the data returned is not
what is expected, it returns

*{"error-code":"Internal Error","error-message":"Content type
'application/x-www-form-urlencoded' not supported","request-id":""}*

What am I doing wrong? Thank you for any help


-- 
Unsubscribe: https://lists.haxx.se/listinfo/curl-library
Etiquette:   https://curl.haxx.se/mail/etiquette.html
Received on 2022-05-23