1

I am trying to build a sample provided in Microsoft Casablanca in my Ubuntu environment. I am following the link: https://msdn.microsoft.com/en-us/library/jj950082.aspx

My code followed from above link:

#include <http_client.h>
#include <iostream>
#include <json.h>

using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace std;
// Retrieves a JSON value from an HTTP request.
pplx::task<void> RequestJSONValueAsync()
{
    // TODO: To successfully use this example, you must perform the request  
    // against a server that provides JSON data.  
    // This example fails because the returned Content-Type is text/html and not application/json.
    http_client client(U("http://www.fourthcoffee.com"));
    return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
    {
        if(response.status_code() == status_codes::OK)
        {
            return response.extract_json();
        }

        // Handle error cases, for now return empty json value... 
        return pplx::task_from_result(json::value());
    })
        .then([](pplx::task<json::value> previousTask)
    {
        try
        {
            const json::value& v = previousTask.get();
            // Perform actions here to process the JSON value...
        }
        catch (const http_exception& e)
        {
            // Print error.
            wostringstream ss;
            ss << e.what() << endl;
            wcout << ss.str();
        }
    });

    /* Output:
    Content-Type must be application/json to extract (is: text/html)
    */
}


int main()
{
    // This example uses the task::wait method to ensure that async operations complete before the app exits.  
    // In most apps, you typically don�t wait for async operations to complete.

    wcout << U("Calling RequestJSONValueAsync..." )<< endl;
    RequestJSONValueAsync().wait();

}

Here i am trying to make GET request to url: http://www.fourthcoffee.com and get JSON data. While i compile and run it using the command in terminal:
sudo g++ -std=c++11 basic_http_client.cpp -o client -lboost_system -lcrypto -lssl -lcpprest
./client

I am facing an error:
Error: Failed to connect to any resolved endpoint.

What could be the possible issue here while making http GET request. Searched Google and SO with no possible solutions.

Satish
  • 1,315
  • 4
  • 15
  • 22

0 Answers0