1

I'm using a restbed c++ and I came across a issue when registering the same uri but with different methods (POST,GET,DELETE) on one resource.

When running the server and requesting a GET request it works fine, but for the POST request I get '405 Method Not Allowed'

PS,Note I had to trick the regex part even to be able to resister the methode.

  • GET : language:[a-z_]
  • POST: language:[ab-z_]
This if I register it with the same expression **set_path("/hello/{language:[a-z_]+}");**
#./so-rest 
Service accepting HTTP connections at 'http://[::]:1984'.
Resource published on route '/hello/{language:[a-z_]+}'.
So I kinda use these paths to be able to register them bought.
#./so-rest
Service accepting HTTP connections at 'http://[::]:1984'.
Resource published on route '/hello/{language:[a-z_]+}'.
Resource published on route '/hello/{language:[ab-z_]+}'.

Any Idea what is going wrong ? Thanks.

This is a sample code.

#include <memory>
#include <cstdlib>
#include <restbed>


using namespace std;
using namespace restbed;


class CustomLogger : public Logger
{
    public:
        void stop( void )
        {
            return;
        }

        void start( const shared_ptr< const Settings >& )
        {
            return;
        }

        void log( const Level, const char* format, ... )
        {
            va_list arguments;
            va_start( arguments, format );
            vfprintf( stderr, format, arguments );
            fprintf( stderr, "\n" );
            va_end( arguments );
        }

        void log_if( bool expression, const Level level, const char* format, ... )
        {
            if ( expression )
            {
                va_list arguments;
                va_start( arguments, format );
                log( level, format, arguments );
                va_end( arguments );
            }
        }
};


int main( const int argc , const char** argv)
{
    Service service;
    auto print_get =  []( const shared_ptr< Session > session ){
            std::string body = "Hello GET";
            session->close( OK, body, { { "Content-Length", ::to_string( body.length( ) ) } } );

        };
    auto print_post =  []( const shared_ptr< Session > session ){
            std::string body = "Hello POST";
            session->close( OK, body, { { "Content-Length", ::to_string( body.length( ) ) } } );

        };

    {
        auto resource = make_shared< Resource >( );
        resource->set_path("/hello/{language:[a-z_]+}");
        resource->set_method_handler( "GET" , print_get);
        service.publish(resource);
    }

    {
        auto resource = make_shared< Resource >( );
        resource->set_path("/hello/{language:[ab-z_]+}");
        resource->set_method_handler( "POST" , print_post);
        service.publish(resource);
    }

    auto settings = make_shared< Settings >( );

    settings->set_port( 1984 );
    service.set_logger( make_shared< CustomLogger >( ) );

    settings->set_default_header( "Connection", "close" );
    service.start( settings );

    return EXIT_SUCCESS;
}
  • How are you invoking the resource endpoints? What client are you using? – Ben Crowhurst Mar 03 '20 at 00:14
  • Hi Thanks for looking into it, I'm using curl. curl --header "Content-Type: application/json" --data "some text" --request POST http://localhost:1984/hello/english And the log is: Incoming 'POST' request from '[::1]:61094' for route '/hello/english'. '[::1]:61094' 'POST' method not allowed '/hello/english'. – Philippe Forest Mar 05 '20 at 23:35
  • Nothing obvious jumps out at me. Could you please raise a ticket on the github page with as much information as possible, thanks. – Ben Crowhurst Mar 08 '20 at 20:35
  • Ok i will Thanks. – Philippe Forest Mar 10 '20 at 16:35
  • Sorry failed to update this post. Short and skinny of the situation is 'language:[a-z_]+' matches 'language:[ab-z_]+' so will never be invoked; fixed in 5.0. – Ben Crowhurst Jul 09 '21 at 08:46

0 Answers0