how to combine the url with c++ rest sdk?

Sep 3, 2014 at 2:56 AM
I want to login this website:
http://221.231.139.132:9999/eps-ws/rest/AccountService/
the function is "login",
there are 4 parameters: name, password, sourceType, description

when I enter this url in IE:
http://221.231.139.132:9999/eps-ws/rest/AccountService/login/test1/123456/1/test
I will get the right result,

here is the c++ program:

http_client client(U("http://221.231.139.132:9999/eps-ws/rest/AccountService/"));
std::wstring functionname = U("login");
std::wstring username = U("test1");
std::wstring password = U("123456");
std::wstring sourceType= U("1");
std::wstring description= U("test");

std::wstring accounturl = functionname + U("/") + username + U("/") + password + U("/") + sourceType+ U("/") + description+ U("/");

return client.request(methods::GET, builder.to_string());

I can get the result but with some problems,
I think the way I use rest SDK must have some problems , help me, Thanks!
Coordinator
Sep 3, 2014 at 9:17 PM
Hey ylongwu,

One small thing I notice with your program is that your accounturl looks like "login/test1/123456/1/test/" -- note the trailing slash. However, this might not be the problem.

Where do you define builder in order to call builder.to_string()?

It looks like your program may be simple enough; could you simply paste the entire program into a message here using the "insert code" format?

Thanks,
roschuma
Sep 4, 2014 at 1:19 AM
Edited Sep 4, 2014 at 1:36 AM
sorry, I have lost the builder part, here is the total program:
void main()
{
    auto fileStream = std::make_shared<ostream>();
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
  {
      http_client client(U("http://221.231.139.132:9999/eps-ws/rest/AccountService/")); 
      //here is the function name and the 4  input parameters of the function:
      std::wstring functionname = U("login"); 
      std::wstring username = U("test1"); 
      std::wstring password = U("123456"); 
      std::wstring sourceType= U("1"); 
      std::wstring description= U("test"); 

      std::wstring accounturl = functionname + U("/") + username + U("/") +
       password + U("/") + sourceType+ U("/") + description+ U("/"); 
      uri_builder builder(accounturl);
      return client.request(methods::GET, builder.to_string()); 
  })


// 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());
  }
}

Coordinator
Sep 4, 2014 at 2:11 AM
Hi again,

I think you've gotten the two streams "fileStream" and "outFile" confused in your code. You never actually open fileStream to point at a file.

Sincerely,
roschuma
Sep 5, 2014 at 6:13 AM
Hi,

I can now get the result, the code is like this:
pplx::task<void> test()
{
    // Create http_client to send the request.
    http_client client(U("http://221.231.139.132:9999/eps-ws/rest/AccountService/"));

    //Build request URI and start the request.
    utility::string_t functionname = U("login");
    utility::string_t username = U("test1");
    utility::string_t password = U("123456");
    utility::string_t sourceType = U("1");
    utility::string_t description = U("test");

    utility::string_t accounturl = functionname + U("/") + username + U("/") + password + U("/") + sourceType + U("/") + description + U("/");
    uri_builder builder(accounturl);

    //task<http_response> aaa = client.request(methods::GET, builder.to_string());

    return client.request(methods::GET, builder.to_string()).then([](http_response response)
    {
        // Display the status code that the server returned
        std::wostringstream stream;
        stream << L"Server returned status code " << response.status_code() << L'.' << std::endl;
        stream << L"Content type: " << response.headers().content_type() << std::endl;
        stream << L"Content length: " << response.headers().content_length() << L"bytes" << std::endl;
        std::wcout << stream.str();

        auto bodyStream = response.body();
        streams::stringstreambuf sbuffer;
        auto& target = sbuffer.collection();

        bodyStream.read_to_end(sbuffer).get();
        stream.str(std::wstring());
        stream << L"Response body: " << target.c_str();
        std::wcout << stream.str();
        getchar();
    });

}
Then called this fun in a main() program, I'm not sure if I'm using the correct method....
Coordinator
Sep 5, 2014 at 9:36 PM
If all you want is to retrieve the body of the response as a string, you might want to look at extract_string():

http://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1http__response.html#a93e27c5454d16545685074faa1dae96b

Sincerely,
roschuma