Toasterson
Toasterson

Reputation: 177

Restbed HTTP Client Empty Body

I tied to implement a REST Client with C++ and the Restbed Library

I tried to copy the code from the example but seem to be missing something.

The Example shows a body but my code does not.

I tried playing with the Headers but no luck.

Can anybody with some knowledge of the library point me in the right direction?

Or suggest me a easy to use C++ REST Library that runs on Solaris?

The Example:

/*
 * Example illustrating a HTTP client.
 *
 * Usage:
 *    ./distribution/example/http_client
 */

#include <memory>
#include <future>
#include <cstdio>
#include <cstdlib>
#include <restbed>

using namespace std;
using namespace restbed;

void print( const shared_ptr< Response >& response )
{
    fprintf( stderr, "*** Response ***\n" );
    fprintf( stderr, "Status Code:    %i\n", response->get_status_code( ) );
    fprintf( stderr, "Status Message: %s\n", response->get_status_message( ).data( ) );
    fprintf( stderr, "HTTP Version:   %.1f\n", response->get_version( ) );
    fprintf( stderr, "HTTP Protocol:  %s\n", response->get_protocol( ).data( ) );

    for ( const auto header : response->get_headers( ) )
    {
        fprintf( stderr, "Header '%s' > '%s'\n", header.first.data( ), header.second.data( ) );
    }

    auto length = 0;
    response->get_header( "Content-Length", length );

    Http::fetch( length, response );

    fprintf( stderr, "Body:           %.*s...\n\n", 25, response->get_body( ).data( ) );
}

int main( const int, const char** )
{
    auto request = make_shared< Request >( Uri( "http://www.corvusoft.co.uk:80/?query=search%20term" ) );
    request->set_header( "Accept", "*/*" );
    request->set_header( "Host", "www.corvusoft.co.uk" );

    auto response = Http::sync( request );
    print( response );

    auto future = Http::async( request, [ ]( const shared_ptr< Request >, const shared_ptr< Response > response )
    {
        fprintf( stderr, "Printing async response\n" );
        print( response );
    } );

    future.wait( );

    return EXIT_SUCCESS;
}

My current Implementation

#include "HttpClient.h"
#include "HTTPException.h"
#include <restbed>
#include <iostream>

using namespace std;
using namespace restbed;

void HttpClient::getVersion_0() {
    auto request = make_shared< Request >( Uri( "http://www.golem.de/" ) );
    request->set_header( "Accept", "*/*" );
    request->set_header( "Cache-Control", "no-cache" );
    request->set_header("Accept-Encoding", "gzip,deflate");
    request->set_header("User-Agent", "PKG6/0.0.1");
    request->set_header("Host", "www.golem.de");
    request->set_header("Connection", "Keep-Alive");
    request->set_method("GET");

    auto response = Http::sync( request );

    int code = response->get_status_code();
/*
    if(code != 200){
        throw pkg::exception::HTTPBadResponseException(code);
    }
*/
    print(response);

    /*
    auto future = Http::async( request, [ ]( const shared_ptr< Request >, const shared_ptr< Response > response )
    {

    } );

    future.wait( );

    */
}


void HttpClient::print(const std::shared_ptr<restbed::Response> &response) {
    fprintf( stderr, "\n*** Response ***\n" );
    fprintf( stderr, "Status Code:    %i\n", response->get_status_code( ) );
    fprintf( stderr, "Status Message: %s\n", response->get_status_message( ).data( ) );
    fprintf( stderr, "HTTP Version:   %.1f\n", response->get_version( ) );
    fprintf( stderr, "HTTP Protocol:  %s\n", response->get_protocol( ).data( ) );

    for ( const auto header : response->get_headers( ) )
    {
        fprintf( stderr, "Header '%s' > '%s'\n", header.first.data( ), header.second.data( ) );
    }

    auto length = 0;
    response->get_header( "Content-Length", length );

    Http::fetch( length, response );

    fprintf( stderr, "Body:           %.*s...\n\n", 25, response->get_body( ).data( ) );
}

Upvotes: 0

Views: 1946

Answers (2)

dparnovskiy
dparnovskiy

Reputation: 79

The first example contains error:

auto length = 0;

response->get_header( "Content-Length", length );

get_header actually returns the header value and second parameter is a default value.

So length is always 0

Upvotes: 2

Toasterson
Toasterson

Reputation: 177

I Switched to Beast Library. It has less samples for Server side code but as a client implementation it is sufficient.

Upvotes: 0

Related Questions