cpprestsdk client/server not working
by chakka.lokesh from LinuxQuestions.org on (#5J6KS)
I refered this link.
written small client and server code
Client code:
Code://g++ -o client client.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include <iostream>
using namespace std;
void make_request( http_client & client, method mtd )
{
client.request(mtd, "/restdemo").then([](http_response response)
{
cout << __LINE__<<" here in get request \n";
if (response.status_code() == status_codes::OK)
{
cout << __LINE__<<" here in get request \n";
cout << response.body();
}
else
{
cout << __LINE__<<" here in get request \n";
}
return pplx::task_from_result(json::value());
}).then([](pplx::task<json::value> previousTask)
{
}).wait();
}
int main()
{
http_client client( "http://localhost" );
make_request( client, methods::GET );
return 0;
}server code:
Code://g++ -o server server.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
#include <iostream>
using namespace std;
void handle_get(http_request request)
{
cout << __LINE__<<" here in get request \n";
request.reply(status_codes::OK, "GET Method result ");
}
int main()
{
http_listener listener("http://localhost/restdemo");
listener.support(methods::GET, handle_get);
try
{
listener
.open()
.then([&listener]() { cout<<"\nstarting to listen\n"; })
.wait();
while (true);
}
catch (exception const & e)
{
cout << e.what() << endl;
}
return 0;
}I am seeing server is not responding for get method.
1. Can someone help where I am doing wrong ?
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?
written small client and server code
Client code:
Code://g++ -o client client.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include <iostream>
using namespace std;
void make_request( http_client & client, method mtd )
{
client.request(mtd, "/restdemo").then([](http_response response)
{
cout << __LINE__<<" here in get request \n";
if (response.status_code() == status_codes::OK)
{
cout << __LINE__<<" here in get request \n";
cout << response.body();
}
else
{
cout << __LINE__<<" here in get request \n";
}
return pplx::task_from_result(json::value());
}).then([](pplx::task<json::value> previousTask)
{
}).wait();
}
int main()
{
http_client client( "http://localhost" );
make_request( client, methods::GET );
return 0;
}server code:
Code://g++ -o server server.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
#include <iostream>
using namespace std;
void handle_get(http_request request)
{
cout << __LINE__<<" here in get request \n";
request.reply(status_codes::OK, "GET Method result ");
}
int main()
{
http_listener listener("http://localhost/restdemo");
listener.support(methods::GET, handle_get);
try
{
listener
.open()
.then([&listener]() { cout<<"\nstarting to listen\n"; })
.wait();
while (true);
}
catch (exception const & e)
{
cout << e.what() << endl;
}
return 0;
}I am seeing server is not responding for get method.
1. Can someone help where I am doing wrong ?
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?