1

How to cout or printf data return from api using c++ rest sdk aka casablanca?

I got this code from tutorial:

#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[])
{
    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://192.168.0.13:3000/api/individual_employment_setting/detail/172"));

        // Build request URI and start the request.
        //uri_builder builder(U("/search"));
        //builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET);
    })

        // Handle response headers arriving.
        .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
        return response.body().read_to_end(fileStream->streambuf());
    })

        // Close the file stream.
        .then([=](size_t)
    {
        return fileStream->close();
    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

    return 0;
}

But it just write a file an .html file.

Is there a way to store the return data of api to a variable and just output it in terminal like cout or printf? Thanks.

noyruto88
  • 767
  • 2
  • 18
  • 40

1 Answers1

1

You can try reading the response body with a string stream buffer, instead of the file stream buffer you're using right now:

    // Handle response headers arriving.
    .then([=](http_response response)
{
    printf("Received response status code:%u\n", response.status_code());

    stringstreambuf buffer;
    response.body().read_to_end(buffer).get();

    //show content in console
    printf("Response body: \n %s", buffer.collection().c_str()); 

    //parse content into a JSON object:
    json::value jsonvalue = json::value::parse(buffer.collection());  

    //write content to file
    return  fileStream->print(buffer.collection());
})
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Thank you. It works now. But do you have an idea how to get the specific data of the return data? The return data is of json type. – noyruto88 Apr 11 '18 at 03:10
  • See the edit I made and take a look at the docs: https://github.com/Microsoft/cpprestsdk/wiki/JSON – p-a-o-l-o Apr 11 '18 at 08:28
  • Does json::value::parse() required namespace? – noyruto88 Apr 11 '18 at 08:55
  • `#include ` – p-a-o-l-o Apr 11 '18 at 09:08
  • There's an error in json::value::parse() it says, "no instance of overload function web::json::value::parse matches the arguments list... I also include cpprest/json.h – noyruto88 Apr 11 '18 at 09:23
  • error C2665: 'web::json::value::parse': none of the 6 overloads could convert all the argument types – noyruto88 Apr 11 '18 at 09:24
  • Example code refers to **10.2** version. Which version are you using (and how did you install it)? – p-a-o-l-o Apr 11 '18 at 09:44
  • I installed it using vcpkg. I guess i'm using the latest version. – noyruto88 Apr 11 '18 at 09:47
  • The version of my cpprestsdk is 2.10.2 – noyruto88 Apr 11 '18 at 09:48
  • @noyruto88 can you please make another question out of this issue? Please post the relevant code in it, and **the whole compiler output**. Thank you very much. – p-a-o-l-o Apr 11 '18 at 09:51
  • okay Sir. Actually I will use cpprestsdk in CLR project of visual c++ with 64 bit config. So, there will be a lot issues. Anyway, thank you very much for your answers on this. I will comment here the new post. Thanks. – noyruto88 Apr 11 '18 at 09:57
  • @paolo, Hi Sir, this is my other issues about cpprestsdk. https://stackoverflow.com/questions/49791269/mutex-is-not-supported-when-compiling-with-clr-or-clrpure-cpprestsdk-aka-casa – noyruto88 Apr 12 '18 at 08:26