简体   繁体   English

使用Casablanca C ++ Rest SDK发送发送接收

[英]Send Receive using casablanca c++ rest sdk

I'm just starting with RESTful programming and trying to make a program in c++ using the Casablanca sdk ( https://github.com/Microsoft/cpprestsdk ). 我只是从RESTful编程开始,并尝试使用Casablanca sdk( https://github.com/Microsoft/cpprestsdk )用c ++编写程序。 I know that I need to use GET, POST, PUT and DEL methods to do data transfer etc. But I cant seem to find any examples on how to do this. 我知道我需要使用GET,POST,PUT和DEL方法进行数据传输等。但是我似乎找不到任何有关如何执行此操作的示例。 I currently need to send an integer value to the server from the client and get a Boolean response from the server. 我目前需要从客户端向服务器发送一个整数值,并从服务器获取布尔响应。 I cant find any good examples in Casablanca's documentation or the web. 我在Casablanca的文档或网络中找不到任何好的示例。 Any help regarding how to do this simple transfer would be appreciated. 任何有关如何进行此简单转移的帮助将不胜感激。

Spending more time to explore the documentation and various examples on the internet would probably have got you the answer. 花更多时间浏览文档和Internet上的各种示例可能会为您找到答案。

Basically, you have to set up a http listener, as the server, that will listen to client request at a particular url. 基本上,您必须将http侦听器(作为服务器)设置为可以在特定url侦听客户端请求的服务器。

Then a client can send data on that url, to communicate with it. 然后,客户端可以在该URL上发送数据,以与其进行通信。

Nevertheless, if you want to exchange data in json format, 不过,如果您想以json格式交换数据,

Server would look something like this 服务器看起来像这样

void handle_post(http_request request)
{
    json::value temp;
    request.extract_json()       //extracts the request content into a json
        .then([&temp](pplx::task<json::value> task)
        {
            temp = task.get();
        })
        .wait();
     //do whatever you want with 'temp' here
        request.reply(status_codes::OK, temp); //send the reply as a json.
}
int main()
{

   http_listener listener(L"http://localhost/restdemo"); //define a listener on this url.

   listener.support(methods::POST, handle_post); //'handle_post' is the function this listener will go to when it receives a POST request.
   try
   {
      listener
         .open()                     //start listening
         .then([&listener](){TRACE(L"\nstarting to listen\n");})
         .wait();

      while (true);
   }
   catch (exception const & e)
   {
      wcout << e.what() << endl;
   }
}

Client would be, 客户

int main()
{
   json::value client_temp;
   http_client client(L"http://localhost");
                                        //insert data into the json e.g : json::value(54)
   client.request(methods::POST, L"/restdemo", object)
                .then([](http_response response)
                {
                    if (response.status_code() == status_codes::OK)
                    {
                        return response.extract_json();
                    }
                    return pplx::task_from_result(json::value());
                })
                    .then([&client_temp ](pplx::task<json::value> previousTask)
                    {
                        client_temp = previousTask.get();
                    })
                    .wait();
}

Your server reply will be stored into 'client_temp' 您的服务器回复将存储在“ client_temp”中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM