Http_Listener with json. I can't access to the json data

Oct 13, 2016 at 4:27 PM
Edited Oct 17, 2016 at 1:22 PM
Hello Everybody,

So I'm lost in my implementation of the REST SDK (v2.8.0 NuGet) for an Http_Listener implemented in my C++ MFC app.

Actually I'm calling perfectly the handlers GET/POST/PUT/DELETE but I can only answer to the requests with strings :
message.reply(status_codes::OK, U("Hello, World! handle_delete"));
I use Postman to send POST requests with some JSON data as you can see:
Image on Imgur

But I tried a lot of times to find a way to work with the JSON data that I recieve. But nothing actually helped me to understand how to achieve it.

Do you have any Http_Listener sample working with json. I can't find any. Because I read these pages :
Using JSON
Old Msdn Doc
OLD EXAMPLE : Full-fledged client-server example with C++ REST SDK 1.1.0
and many other documentations on this website...

So I don't know what to use as functions or anything else to get the value from the json, make some actions in my MFC app and then send an answer in json.

There is a huge lack of precise documentation on the Http_listener. With the 1 or 2 examples we can see how it works globally. But if I want to implement for example json. Nothing appear on this website after a lot of searches...

So please give me any help to find how can I process recieved json data and how can I send some as answer.

Thank you a lot for your help.
Sincerely, Ahmed
Oct 17, 2016 at 1:15 PM
Edited Oct 17, 2016 at 1:24 PM
Hello All.
I'm waiting for your answers. I tried to read more and more things about it but the most I read the most I get stuck. So this is a code snippet about where I'm blocked now:
void CMFCApplication1Dlg::handle_post(http_request Recieved_request)
{
    
    // /!\ here I want to get the json content of the recieved request but I can't... /!\ 
    pplx::task< json::value > req;

    req = Recieved_request.extract_json(false);
    //I need help here. How can I get the json values. This "req" is a task and don't contain json informations 
    // /!\ end of the work on the recieved POST request with json data.

    //I'm replying to the POST request with some json Data:
    json::value response;
    response[U("Success")] = json::value::string(L"Yes");
    response[U("Cmd")] = json::value::string(L"Erase_initial");
    response[U("Id")] = json::value::string(L"stv13");
    //end of the work on the response

    Recieved_request.reply(status_codes::OK, response);
};
So now I know how to send json data but I'm still blocked about how can I retrieve it in this Http_Listener.

All what I find is about clients and I can't implement it because the examples are too old and deprecated.

So please help me as much as you can and tell me how can I get the different json values from the http_request for my Listener.


Thank you all.
Sincerely, Ahmed
Jan 18 at 3:11 PM
Edited Jan 19 at 7:42 AM
You are pretty close. Now you just have to use the .then() method of the task as follows:
void handle_post(http_request Recieved_request)
{
    auto task = Recieved_request.extract_json(false).then([=](json::value extracted_json) {
        // do whatever you want with the extractet json. For example read the 'name' field
        std::wstring name = extracted_json.at(L"name").as_string();
        // reply
        message.reply(status_codes::OK);
    });
};
Marked as answer by WyNot_1 on 2/10/2017 at 2:46 AM
Feb 10 at 10:48 AM
Hello Speins and thank you for your answer!