meltwater / served

A C++11 RESTful web server library
MIT License
710 stars 174 forks source link

Unable to use same endpoint for different HTTP methods #21

Closed nabroyan closed 7 years ago

nabroyan commented 7 years ago

Hello. I'm using served library and I really like it. However, I've encountered with an issue. It is not possible to use same endpoint for different HTTP methods. It seems like the latter registered method replaces the previous one and I get the following error.

Method not allowed

A simple code to reproduce the issue

// Standard C++ includes 
#include <stdlib.h>
#include <iostream>

// REST
#include <served/served.hpp>
#include <served/plugins.hpp>

int main(int argc, char const* argv[])
{
    try 
    {
        // Create a multiplexer for handling requests
        served::multiplexer mux;

        // GET
        mux.handle("/hello")
            .get([&](served::response& res, const served::request& req) {
                res << "Hello from GET\n";
        });

        // POST
        mux.handle("/hello")
            .post([&](served::response& res, const served::request& req) {
                res << "Hello from POST\n";
        });

        // Register served::plugins::access_log plugin to run after a handler
        mux.use_after(served::plugin::access_log);

        // Create the server and run with 10 handler threads.
        served::net::server server("0.0.0.0", "8080", mux);
        server.run(10);
    } 
    catch (const std::exception& e)
    {
        std::cout << e.what();
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

`

cjgdev commented 7 years ago

The correct way to handle different methods is like this:

mux.handle("/hello")
    // GET
    .get([&](served::response& res, const served::request& req) {
        res << "Hello from GET\n";
    })
    // POST
    .post([&](served::response& res, const served::request& req) {
        res << "Hello from POST\n";
    })
;
nabroyan commented 7 years ago

That works fine. Thank you very much!