How to solve Error “Incorrect Content-Type: must be textual to extract_string, JSON to extract_json.” in c++

While I was accessing Json webservice from C++ using “C++ rest sdk” ,I came across the error
“Incorrect Content-Type: must be textual to extract_string, JSON to extract_json.”

Reason was The webservice I was calling using “GET” API,Its response Content-type was “text/plain” ,So when I was trying to extract Json data from this response ,I was getting above error.

To avoid the error, we have to  set the Content-Type of response  to application/json explicitly Like following :


pplx::task RequestJSONValueAsync()
{
// Declare webservice to consume json data from it
http_client client(L"webservice url");
//Set parameters for request
http_request req(methods::GET);
req.headers().set_content_type(L"application/json");


//Fire request on webservice and check the response
return client.request(req).then([](http_response response) -> pplx::task
{
if (response.status_code() == status_codes::OK )
{
//set content type json
response.headers().set_content_type(L"application/json");
return response.extract_json();
}

// Handle error cases, for now return empty json value...
return pplx::task_from_result(json::value());
})
.then([](pplx::task previousTask)

{
try
{
//get json value from response
const json::value& v = previousTask.get();

string_t jsonval = v.serialize();
wcout << (L"Extracted json data from webservice") << endl;
//Display extracted json from webservice
wcout << jsonval;


}
catch (const http_exception& e)
{
// Print error.
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});

}
int wmain()
{
// This application uses the task::wait method to ensure that async operations complete before the
// app exits.

cout << L"Calling RequestJSONValueAsync..." << endl;
RequestJSONValueAsync().wait();

system("pause");
return 0;
}

To get complete code and understanding of consuming json webservice through c++ ,Refer below link
link

Ambika Sahni: