C++ REST SDK Casablanca Client.request
Solution 1
It is possible that the main thread is terminated before the "request" task completed, so you cannot see any console outputs. I suggest you to call the task "wait()" function after ".then", like in the answer on their site
Solution 2
Your program runs off the end of main
and terminates. You need to add wait
after the then
call:
client.request(methods::GET).then([](http_response response)
{
// ...
}).wait();
Solution 3
this code is working :
// ConsoleApplication1.cpp : Defines the entry point for the console application.
#include "StdAfx.h"
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
int main(int argc, char* argv[])
{
// Make the request and asynchronously process the response.
http_client client(L"http://localhost:8082/TPJAXRS/Test/test");
client.request(methods::GET).then([](http_response response){
if(response.status_code() == status_codes::OK){
auto body = response.extract_string().get();
std::wcout << body<< std::endl;
}});
std::cout << "Hello world!" << std::endl;
system("PAUSE");
return 0;
}
user3580493
Updated on June 04, 2022Comments
-
user3580493 almost 2 years
I want to write a little c++ program that sends a request to a server an get some data. I found the C++ Rest-SDK and decided to use it. I searched on different websites for code-examples but many of them doesn't work an shows syntax errors. What i got now is that code but the client.request method is skipped. The program never jumps in. Hope someone can realise the problem and maybe explain what i have to change.
#include <Windows.h> #include <iostream> #include <sstream> #include <string> #include "cpprest/containerstream.h" #include "cpprest/filestream.h" #include "cpprest/http_client.h" #include "cpprest/json.h" #include "cpprest/producerconsumerstream.h" #include "cpprest/http_client.h" #include <string.h> #include <conio.h> using namespace std; using namespace web; using namespace web::json; using namespace web::http; using namespace web::http::client; using namespace utility; using namespace utility::conversions; int main() { http_client client(L"http://httpbin.org/ip"); client.request(methods::GET).then([](http_response response) { if(response.status_code() == status_codes::OK) { auto body = response.extract_string().get(); std::wcout << body; getch(); } }); return 0; }