Corvusoft / restbed

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

Simple HTTP Server Hello World example returns code 501 #509

Closed mcordova1967 closed 2 years ago

mcordova1967 commented 2 years ago

Hello, I am using the library on Win x64 with MingW, I installed restbed and asio using MSYS2 packages and the example compiles and runs, but then I try to navigate to http:://localhost:8080/resource using Chrome a 501 is returned.

compile/link command: g++ -O3 -std=c++20 restbed.cpp -o restbed.exe -lrestbed

Any help is much appreciated. Regards, Martin

The code:

include

include

include

using namespace std; using namespace restbed;

void post_method_handler( const shared_ptr< Session > session ) { const auto request = session->get_request( );

int content_length = request->get_header( "Content-Length", 0 );

session->fetch( content_length, [ ]( const shared_ptr< Session > session, const Bytes & body )
{
    fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
    session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );

}

int main( const int, const char** ) { auto resource = make_shared< Resource >( ); resource->set_path( "/resource" ); resource->set_method_handler( "POST", post_method_handler );

auto settings = make_shared< Settings >( );
settings->set_port( 8080 );
settings->set_default_header( "Connection", "close" );

Service service;
service.publish( resource );
service.start( settings );

return EXIT_SUCCESS;

}

ben-crowhurst commented 2 years ago

You've set an HTTP POST method handler, Chrome will issue an HTTP GET request. This won't be found and is not implemented anywhere else on the service hence the 501.