RESTful Client API in C++

后端 未结 4 1667
深忆病人
深忆病人 2021-01-18 05:26

Is there any open source library available that implements RESTful Client(library for interpreting HTTP requests as REST service calls) in C++ ?

My requirement is to

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 06:13

    Restbed offers synchronous and asynchronous HTTP/ HTTPS client capabilities.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    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;
    }
    

提交回复
热议问题