C++ REST GET does not return data
Fred Young
1
Reputation point
Using C++ and Visual Studio 2022, cpr REST GET is not returning data. Status code is 0 but data not returned. The server log shows the GET operated correctly on the server. The URL, when tested from a command prompt with "curl" returns the data, but the C++ program using cpr does not.
string rest_get(string url)
{
ofstream CrnActLog;
CrnActLog.open("..\CrnAct.log", ofstream::out | ofstream::app);
CrnActLog << GetTimeNow() << "- " <<"Initiating GET call to " << url << endl;
Response r = cpr::Get(Url{ url.c_str() },
Authentication{ "restService", "rest@pf" },
Parameters{ {"anon", "true"}, {"key", "value"} });
CrnActLog << GetTimeNow() << "- " << "r.status_code " << r.status_code << endl;
CrnActLog << GetTimeNow() << "- " << "r.header[content-type] " << r.header["content-type"] << endl;
CrnActLog << GetTimeNow() << "- " << "r.text " << r.text << endl;
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text; // JSON text string
Json::Value root;
int page = root["page"].asInt();
Json::Value total = root["total"];
Json::Value data = root["data"];
CrnActLog << GetTimeNow() << "- " <<"rest_call returned " << r.text << endl;
CrnActLog.close();
return r.text;
}
Sign in to answer