1

I am new to C++ REST ('Casablanca'). I read the tutorial here. After than, I took a sample code from there and tried to run it on my machine.

Below is the code

std::map<utility::string_t, utility::string_t> dictionary;

void handle_get(http_request request)
{
    TRACE(L"\nhandle GET\n");

    web::json::value::field_map answer;

    for (auto const & p : dictionary)
    {
        answer.push_back(std::make_pair(json::value(p.first), json::value(p.second)));
    }

    request.reply(status_codes::OK, json::value::object(answer));
}

int main()
{
    http_listener listener(L"http://127.0.0.1:8080/stockData");

    listener.support(methods::GET, handle_get);

    return 0;
}

In this code, I am getting error as below

enter image description here

I checked the header file json.h and could not find a member (class/struct) named field_map Please help

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • The tutorial is from 2013. So. When was your version of the library released? – underscore_d Aug 08 '16 at 11:56
  • @underscore_d I have taken library from NuGet version CPP REST 140 v2.8.0 and I am using VS 2015 – SimpleGuy Aug 08 '16 at 12:02
  • Do you expect me to go and look up when that version was released, or can you just look at the copy you already have (e.g. the timestamps, changelog, etc.) and tell us? – underscore_d Aug 08 '16 at 12:03
  • @underscore_d Sorry, Nov 23, 2015 – SimpleGuy Aug 08 '16 at 12:04
  • Thanks. Well, because your version is newer, perhaps the member `field_map` has been deprecated and removed since that tutorial was written. If so, you will simply need to consult the newer documentation and see whether there are any notes about a replacement or some other way to do the same thing, or search for discussions where others might've talked about the removal and how to adjust for it. – underscore_d Aug 08 '16 at 12:05
  • @underscore_d Hmm... Thanks, I will try to search for the alternative.. though no luck so far.. – SimpleGuy Aug 08 '16 at 12:13

1 Answers1

6

I think below code can replace your code and should be compile latest stable version cpprestsdk v2.8.0

std::map<utility::string_t, utility::string_t> dictionary;

void handle_get(http_request request)
{
    TRACE(L"\nhandle GET\n");

    json::value obj;
    for ( auto const & p : dictionary )
    {
        obj[p.first] = json::value::string(p.second);
    }

    // this is just for debugging
    utility::stringstream_t stream;
    obj.serialize(stream);
    std::wcout << stream.str();

    request.reply( status_codes::OK, obj);
}
hyun
  • 2,135
  • 2
  • 18
  • 20