Corvusoft / restbed

Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
http://www.corvusoft.co.uk
Other
1.93k stars 377 forks source link

http client post method #543

Closed arifrahim-edu closed 1 year ago

arifrahim-edu commented 1 year ago

Hi, I am trying to implement an HTTP_Client following your example. But I need to use POST method. Below is the cpp class

include <memory>
include <restbed>
include <vector>
include <chrono>
include <cstdio>
include <cstdlib>

using namespace std;
using namespace restbed;
namespace sc = std::chrono;

class restbedClient{

    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 = response->get_header( "Content-Length", 0 );

        Http::fetch( length, response );

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

    public: int restbed_client(const string& server_ip, const string& path, const string& json_string) 
    {

        shared_ptr<Request> request = make_shared<Request>(Uri("http://" + server_ip + path));

        // Set the request method
        // additionally set to see if they have any influence 
        request->set_header("Accept","*/*");
        //request->set_host("127.0.0.1");
        //request->set_port(8080);
        //request->set_path(path);
        //request->set_version(1.1);
        request->set_method("POST");

        // these are the requirmeents
        request->set_header("Content-Type", "appliction/json");
        request->set_body(json_string);

        auto settings = make_shared< Settings >( );
        settings->set_connection_limit( 3000 );

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

        return EXIT_SUCCESS;

    }

};

    int main(){
    restbedClient rbc;
    std::string json_string = "{\"name\":\"John Doe\",\"email\":\"johndoe@example.com\"}";
    int exitStatus = rbc.restbed_client("127.0.0.1","/api/nia", json_string);
}

// // compile : clang++ -std=c++17 -o client.bin post_client.cpp -l restbed // execute : ./client.bin //

/* output is :

./client.bin Response Status Code: 0 Status Message: Error HTTP Version: 1.1 HTTP Protocol: HTTP Header 'Content-Length' > '50' Header 'Content-Type' > 'text/plain; utf-8' terminate called after throwing an instance of 'std::invalid_argument' what():
Aborted

*/

ben-crowhurst commented 1 year ago

The HTTP client is deprecated and shouldn't be used.