VS2010 MFC calling MFC4 APIs : Simple Samples

Jun 25, 2013 at 8:59 PM
I have done C++ and C# for years, but I am relatively new to task based development and C++ lambda expressions.

I am looking for some simple samples to help to to send requests to MVC4 Web APIs on local server. Code base for client is C++ MFC vs2010. Is Casablanca the right thing for this? I do not necessarily need async calls right now.

Any help would be appreciated. Thanks!
Jun 25, 2013 at 11:14 PM
You are at the right place. With Casablanca, you can connect to HTTP servers, send HTTP requests and receive responses from any C++ application.

Most of the APIs return a task (an asynchronous operation), you can use the task::get method to wait for the task to finish and obtain the result of the task.
A simple example of how to send a HTTP GET request is:
web::http::client::http_client client(U("http://localhost:80"));
pplx::task<web::http::http_response> resp = client.request(web::http::methods::GET, U("/foo.html"));

web::http::http_response response = resp.get();
Here, the http_client::request method returns a task. This task produces a result of type http_response upon successful completion.
Calling .get() on the task will wait for the request to be sent and the response to be received and returns the response.

We also have some How To guides that demonstrate how to send HTTP GET and PUT requests on msdn.
These samples demonstrate how to asynchronously send the request and process the response using continuations (task::then method)

Let me know if you have any other questions.

Thanks,
Kavya
Jun 26, 2013 at 4:43 PM
Thanks!
Now can you show me how to use extract_json() to get response value as json (or XML) into a string?
This is what I have so far, but I get an exception. My guess is I need to do a .Then or . wait somewhere for the extract_json task?
Or is there another (better?) way to do it without task?
CString CRestRequest::RequestSimpleTest()
{
    CString strResponse;

    web::http::client::http_client client(U("http://www.fourthcoffee.com"));
    //pplx::task<web::http::http_response> resp = client.request(web::http::methods::GET, U("/foo.html"));
    pplx::task<web::http::http_response> resp = client.request(web::http::methods::GET);

    web::http::http_response response = resp.get();

    if (response.status_code() == status_codes::OK)
    {
        pplx::task<json::value> jsonTask = response.extract_json();

        web::json::value jsonValue = jsonTask.get();
        string_t jsonString = jsonValue.to_string();

        strResponse = jsonString.c_str();
    }
    return strResponse;
}
Jun 26, 2013 at 7:35 PM
response.extract_json() returns a task and calling jsonTask.get() will wait for the task to complete and returns the result of the task.
In this case, the content type received from www.fourthcoffee.com is not a JSON type, hence the extract_json task is throwing an exception.

If you place a try catch block while processing the response, the exception is of type http::http_exception and the exception message reads: "Content-Type must be JSON to extract (is: text/html)"

Here is an example of how you could look at the content-type from the response headers and use extract_json or extract_string to read the content body:
    if (response.status_code() == status_codes::OK)
    {
        try
        {
            string_t content_type = response.headers().content_type();
            std::wcout << content_type;

            if (0 == content_type.compare(L"application/json"))
            {
                pplx::task<json::value> jsonTask = response.extract_json();

                web::json::value jsonValue = jsonTask.get();
                string_t jsonString = jsonValue.to_string();

                strResponse = jsonString.c_str();
            }
            else if (0 == content_type.compare(L"text/html"))
            {
                pplx::task<string_t> stringTask = response.extract_string();
                string_t htmlString = stringTask.get();

                strResponse = htmlString.c_str();
            }
        }
        catch(http::http_exception ex)
        {
            std::cout << ex.what();
        }
    }