C++ REST SDK Casablanca Client.request

user3580493 picture user3580493 · Apr 28, 2014 · Viewed 11.9k times · Source

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;
}

Answer

thedarkside ofthemoon picture thedarkside ofthemoon · Apr 30, 2014

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