Error reading characters of string.

Jan 21, 2015 at 2:56 PM
Hello

My goal is to get json data from API

I get the following error
'Error reading characters of string.',
But status comes Ok (200) so I think problem related with json parsing

Please help.
This is my code.
http_client client(L"link");

        http_request request(methods::GET);
        request.headers().add(L"Accept", L"application/json");
    

        return client.request(request).then([](http_response response) -> pplx::task<json::value>
        {
            if (response.status_code() == status_codes::OK)
            {
                return response.extract_json();
            }

            // Handle error cases, for now return empty json value... 
            return pplx::task_from_result(json::value());
        })
            .then([](pplx::task<json::value> previousTask)
        {
            try
            {
                const json::value& v = previousTask.get();
                extract_json_issues(v);
                // Perform actions here to process the JSON value...
            }
            catch (const http_exception& e)
            {
                char * error = const_cast<char*>(e.what());
                // Error reading characters of string.
            }
        });
Coordinator
Jan 21, 2015 at 8:52 PM
Hi ggiloyan89,

What is the std::error_code value on the http_exception class? You can access it from http_exception::error_code().

Steve
Jan 22, 2015 at 7:26 AM
Hi Steve

std::error_code value is 0
std::error_code message is 'Error reading characters of string'

But during the debugging when I mouse over on variable error I see the following message
Content-Type must be JSON to extract (is: application/hal+json)

Please advise.
Jan 22, 2015 at 7:27 AM
Edited Jan 22, 2015 at 7:27 AM
stevetgates wrote:
Hi ggiloyan89,

What is the std::error_code value on the http_exception class? You can access it from http_exception::error_code().

Steve
Hi Steve

std::error_code value is 0
std::error_code message is 'Error reading characters of string'

But during the debugging when I mouse over on variable error I see the following message
Content-Type must be JSON to extract (is: application/hal+json)

Please advise.
Coordinator
Jan 22, 2015 at 4:44 PM
Hi ggiloyan89,

You mention the exception error message says Content-Type must be JSON to extract. That is the exact problem then. The HTTP response isn't including a content type header that we recognize and know what to do with. If you are sure that the response body contains UTF-8 JSON you can force the extract_json method to ignore the content type header. Extract_json takes a bool, try calling extract_json(true).

Steve
Marked as answer by ggiloyan89 on 1/22/2015 at 12:20 PM
Jan 22, 2015 at 7:21 PM
stevetgates wrote:
Hi ggiloyan89,

You mention the exception error message says Content-Type must be JSON to extract. That is the exact problem then. The HTTP response isn't including a content type header that we recognize and know what to do with. If you are sure that the response body contains UTF-8 JSON you can force the extract_json method to ignore the content type header. Extract_json takes a bool, try calling extract_json(true).

Steve
Also I can change content_type to 'application/json' after getting the response but your version is more professional! Thanks