http_client failing to parse a JSON string

Nov 30, 2014 at 12:19 AM
Hello,
I am using the latest version of Casablanca and the JSON parser is failing to read the output from a third party REST service. The raw response is:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 9
Content-Type: application/x-javascript; charset=utf-8
Expires: -1
Date: Sun, 30 Nov 2014 00:13:45 GMT

"Hello"
When I parse it I get "Malformed token". Any text within double quotes is actually a valid JSON string token. It looks like the extractor is removing the double quotes before parsing the JSON.

Do I miss anything?

Thanks,
Pedro
Coordinator
Dec 3, 2014 at 3:09 AM
Hi Pedro,

I don't have any trouble parsing a valid JSON string. Do you have any code you can reproduce the issue with? The following works just file parsing a single JSON string token. Many of our tests cover this, in Release\tests\functional\json\parsing_tests.cpp.
auto v = web::json::value::parse(U("\"String\""));
Steve
Dec 4, 2014 at 5:14 AM
Hi Steve,
I don't think this is a JSON parse problem, I think it is a problem of the HTTP client. When the response is a valid JSON returning "Hello", it looks like the HTTP client, perhaps extract_json(), is removing the double quotes before passing it to the JSON parser.

Thanks,
Pedro
Coordinator
Dec 4, 2014 at 5:52 PM
Hi Pedro,

I also tried writing a test with a server sending an HTTP response containing the JSON string, like in your example. This worked fine. The following code run parses the json string value just fine and prints json string:"Hello"
    web::http::experimental::listener::http_listener listener(m_uri);
    listener.open().wait();
    http_client c(m_uri);

    listener.support([](http_request request)
    {
        http_response response(status_codes::OK);
        response.set_body("\"Hello\"", "application/x-javascript; charset=utf-8");
        request.reply(response).wait();
    });

    auto response = c.request(methods::GET).get();
    auto jsonValue = response.extract_json().get();
    auto jsonSerialized = jsonValue.serialize();
    wprintf(L"json string:%s\n", jsonSerialized.c_str());

    listener.close().wait();
What is the code you are using? Do you have a repro that you can share?

Thanks,
Steve