Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPPRestSDK (casablanca) Extract JSON From Incoming WebSocket Messages (malformed token)

I'm connecting to a WebSocket whom always replies in JSON. I see there is an extract_string method for websocket_incoming_message however after trying numerous things with json:value it seems as though you can only construct JSON arrays on-the-fly by inserting key-value pairs one-by-one. Am I missing something here or is there a way to take the output from websocket_incoming_message and directly convert it into a json:value array?

    wsClient.set_message_handler([=](websocket_incoming_message msg)
    {
        // handle message from server...
        printf("[WebSocket INBOUND]: %s", msg.extract_string().get().c_str());
        printJSON(json::value::parse(conversions::to_string_t(msg.extract_string().get())));
    });

printJSON runs through the json::value and prints each key-value-pair.

Unhandled exception at 0x00007FF866923FB8 in RestAPI.exe: Microsoft C++ exception: web::json::json_exception at memory location 0x0000003E553FDDC0. occurred

Console Output:

[WebSocket INBOUND]: {"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["gateway-prd-main-cr3x"]}}

Even though we can compile and run the application, I figure the exception is being caused due to the fact that were passing a string containing a JSON Table and not a single element? Does this mean I need to manually parse the string and pull out each key-value-pair while simultaneously building the json array?

There must be a way to do this, it seems like basic needed functionality..

A similar unresolved question

Any help here would be greatly appreciated! Thank you for your time.

like image 580
KKlouzal Avatar asked Nov 03 '17 15:11

KKlouzal


1 Answers

Try catching web::json::json_exception and print the message, it may give you a hint about what's wrong

like image 115
hks Avatar answered Oct 15 '22 03:10

hks