Error: Cannot return the string in a http_listener for c++ rest sdk

Jul 4, 2014 at 1:17 PM
Error: string_t(to_wstring(sum1)) this is not working..

Server.cpp
1>c:\users\amu\desktop\server\server\server.cpp(32): error C2665: 'std::to_wstring' : none of the 9 overloads could convert all the argument types
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(644): could be 'std::wstring std::to_wstring(float)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(639): or 'std::wstring std::to_wstring(double)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(626): or 'std::wstring std::to_wstring(long double)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(618): or 'std::wstring std::to_wstring(_ULonglong)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(610): or 'std::wstring std::to_wstring(_Longlong)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(602): or 'std::wstring std::to_wstring(unsigned long)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(594): or 'std::wstring std::to_wstring(long)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(586): or 'std::wstring std::to_wstring(unsigned int)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\string(578): or 'std::wstring std::to_wstring(int)'
1> while trying to match the argument list '(std::string)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>             
#include <cpprest/json.h>                      
#include <cpprest/uri.h>                                
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios
#include <iostream>

using namespace std;
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
using namespace web::http::experimental::listener;

std::string sample1()
{
    string text = "hello world";
    return text;
}

int main()
{
    http_listener listener(L"http://localhost:8080/hello");
    listener.support(web::http::methods::GET, [](web::http::http_request request)
    {
        string sum1 = sample1();
        string_t msg = U("\nServer-side Process \nuri:") + request.request_uri().to_string() + U(" \nTime:") + string_t(to_wstring(time(nullptr))) + U(" \nReturn the String = ") + string_t(to_wstring(sum1));
        wcout << msg << endl;
        request.reply(web::http::status_codes::OK, msg);
    }
    );
    listener.open().wait();

    std::string line;
    std::cout << "Press enter to exit" << std::endl;
    std::getline(std::cin, line);

    listener.close().wait();
    
    getchar();
    return 0;
}

Please Give me the solution.. Help me..

Thanks,
Bharathij
Jul 5, 2014 at 7:27 AM
Hi,

Please Help me.. Very urgent..

Thanks;
Pari
Jul 5, 2014 at 7:02 PM
Hi Bharathij,

It is the 4th of the July holiday in the US so most of the team is on vacation, responses are going to be delayed. I'm not back in the office until Tuesday.

The function std::wstring isn't part of the C++ Rest SDK. It looks like you are trying to convert a time_t object to a string. All of our string utility functions are located in asyncrt_utils.h, so you can take a look in there. I'm not sure of the top of my head to convert a time_t to a string. You could take a look at the date_time class. I think you could use something like ::utility::date_time::utc_now().to_string().

Steve
Jul 7, 2014 at 6:07 AM
Edited Jul 7, 2014 at 2:21 PM
Hi Steve,

Thank you for your reply.. steve.. I can't understand for that one.. so please give me a correct code.. steve.. Help me..
I have one more doubt in this link.. https://casablanca.codeplex.com/discussions/550326.. please steve help me..

Thanks,
Bharathij
Jul 7, 2014 at 5:15 PM
Hi Bharathij,

Replace your std::to_wstring(time(nullptr)) with ::utility::date_time::utc_now().to_string(). Either that or I recommend you take a look at using chrono from the C++ standard library..

Steve