POST request to C++ Rest (casablanca) server

599 views Asked by At

I am new to full stack development and have few queries, I am working on a project (basically for self learning) in which I want to create a front end using node/express js and at backend want to use a mysql database. Connection between my website and database will be done through a rest server. This rest server will be implemented in Casablanca and will also have logic regarding how to handle database.

So my main concern is how could I handle post request in Casablanca rest server? Motive is to submit form at my site created using node/express js. Forward the value received through form (i.e. through node js rest client) towards Casablanca Rest server which will further update the database.

How could I handle such (post) request in casablanca and fetch values? I have tried few things to make this work and the latest that worked to some extent is mentioned below:

To support HTTP POST request below line is added in the code or listener is registered with POST method:

listener.support(methods::POST, handle_post);

And in handle_post method I am trying to extract the json through http_request::extract_json Method, as below:

void handle_post(http_request request)
{
    try
    {
            json::value v = request.extract_json().get();
            someFunction(v);                              //to iterate over JSON and update database
    }
    catch(http_exception const & e)
    {
            std::wcout << e.what() << std::endl;
    }

}

After this when I sends a POST request (form Postman plugin of chrome), I am getting 500 internal error, even my someFunction is not getting called. Can someone please provide a clue of what exactly I am doing wrong here?

It could be possible that my whole implementation approach is wrong but it will really be very helpful if someone could provide me some pointers in the right direction.

Thanks in advance :)

1

There are 1 answers

0
Mitch On

You need to reply to the client with a status code

request.reply(status_codes::OK, U("Hello World!"));

just swap the hello world for the values you want to return from the server.