C + + REST SDK I

Source: Internet
Author: User

welcome!

The C + + REST SDK is a Microsoft project for cloud-based client-server Communication in native code using a modern Asynchro Nous c + + API design. This project aims to help C + + developers connect to and interact with services.

Getting Started






With Vcpkg on Windows

PS> vcpkg install cpprestsdk cpprestsdk:x64-windows

With Apt-get on Debian/ubuntu

$ sudo apt-get install libcpprest-dev

With Brew on OSX

$ brew install cpprestsdk

With NuGet on Windows for Android

PM> Install-Package cpprestsdk.android

For the other platforms, the install options, how-to build from source, and more, take a-look at our documentation.

Once You has the library, look at your tutorial to use the http_client. It walks through how to setup a project to use the C + + Rest SDK and make a basic Http request.

To use from CMake:

Cmake_minimum_required (VERSION 3.7)Project (main)required)add_executable (main main.cpp)  PRIVATE cpprestsdk::cpprest)    
What ' s in the SDK:
    • Features-http Client/server, JSON, URI, asynchronous streams, WebSockets client, OAuth
    • PPL tasks-a powerful model for composing asynchronous operations based on C + + one features
    • Platforms-windows Desktop, Windows Store (UWP), Linux, OS X, Unix, IOS, and Android
    • Support for Visual Studio, with debugger visualizers
Contribute back!

Is there a feature missing so you ' d like to see, or found a bug which you had a fix for? Are you having an idea or just interest in helping out in building the library? Let us know and we ' d love to work with you. For a good the starting point in where we are headed and feature ideas, take a look at our requested features and bugs.

Big or small we ' d like-to-take your-contributions back-to-help improve the C + + Rest SDK for everyone. If interested Contact us askcasablanca at Microsoft dot com.

Having trouble?

we ' d love to get your review score, whether good or bad, but even more than that, we want to fix your problem. If you submit your issue as a Review, we won ' is able to respond to your problem and ask any follow-up questions be necessary. The most efficient-to-do-is-to-open a issue in our issue tracker.

Quick Links
    • FAQs
    • Documentation
    • Issue Tracker
    • Directly Contact Us: [email protected]

This project has adopted the Microsoft Open Source Code of conduct. For more information see the Code of Conduct FAQs or contact [e-mail protected] with any additional questions or co Mments.

Install Microsoft's Open source Cpprestsdk (c + + REST SDK (codename "Casablanca")), there is a project, here a new WIN32 console project, called XXX, the default use of system-generated code;

Then open: Package Manager, VS2013, tools----Library Package Manager console

Input:

Install-package CPPRESTSDK

Wait for the installation to complete;

Or slow, to https://www.nuget.org/packages?q=cpprestsdk.v120.

Manually download these packages (click in, point download) to the cache directory: C:\Users\Administrator\AppData\Local\NuGet\Cache

Re-execute Install-package cpprestsdk

Waiting for installation

Show

。。。

Successfully added "Cpprestsdk 2.9.1.1" to xxx (the name of your new project) and the installation was successful.

Replace the code that contains the main file with the following example:

[CPP]View PlainCopy
  1. Xx.cpp: Defines the entry point of the console application.
  2. //
  3. #include "stdafx.h"
  4. /*
  5. int _tmain (int argc, _tchar* argv[])
  6. {
  7. return 0;
  8. }
  9. */
  10. #include <cpprest/http_client.h>
  11. #include <cpprest/json.h>
  12. #include
  13. #include <iostream>
  14. #include <json.h>
  15. Using namespace web;
  16. Using namespace Web::http;
  17. Using namespace Web::http::client;
  18. Using namespace std;
  19. Retrieves a JSON value from an HTTP request.
  20. pplx::task<void> requestjsonvalueasync ()
  21. {
  22. //Todo:to Successfully Use this example, you must perform the request
  23. //against a server that provides JSON data.
  24. //This example fails because the returned Content-type are text/html and not application/json.
  25. //http_client Client (L "http://www.fourthcoffee.com");
  26. Http_client Client (L"http://www.fourthcoffee.com");
  27. return Client.request (methods::get). then ([] (http_response response), pplx::task<json::value>
  28. {
  29. if (response.status_code () = = Status_codes::ok)
  30. {
  31. wcout<< response.extract_string (). Get (). C_STR () <<endl;
  32. return Response.extract_json ();
  33. }
  34. //Handle error cases, for now return empty JSON value ...
  35. return Pplx::task_from_result (Json::value ());
  36. })
  37. . then ([] (pplx::task<json::value> previoustask)
  38. {
  39. Try
  40. {
  41. Const json::value& v = previoustask.get ();
  42. //Perform actions here to process the JSON value ...
  43. }
  44. catch (const http_exception& e)
  45. {
  46. //Print error.
  47. Wostringstream SS;
  48. SS << E.what () << Endl;
  49. Wcout << ss.str ();
  50. }
  51. });
  52. /* Output:
  53. Content-type must be Application/json to extract (is:text/html)
  54. */
  55. }
  56. Demonstrates how to iterate over a JSON object.
  57. void Iteratejsonvalue ()
  58. {
  59. //Create a JSON object.
  60. Json::value obj;
  61. Obj[l"Key1"] = Json::value::boolean (false);
  62. Obj[l"Key2"] = Json::value::number (44);
  63. Obj[l"Key3"] = Json::value::number (43.6);
  64. Obj[l"Key4"] = json::value::string (U ("str"));
  65. //Loop over per element in the object.
  66. For (Auto iter = Obj.as_object (). Cbegin (); ITER! = Obj.as_object (). Cend (); ++iter)
  67. {
  68. //Make sure to get the value as const reference otherwise you'll end up copying
  69. //The whole JSON value recursively which can be expensive if it is a nested object.
  70. //const json::value &str = iter->first;
  71. //const json::value &v = iter->second;
  72. Const Auto &STR = iter->first;
  73. Const Auto &V = iter->second;
  74. //Perform actions here to process each string and value in the JSON object ...
  75. Std::wcout << L"String:" << str.c_str () << L", Value:" << v.serialize () << Endl;
  76. }
  77. /* Output:
  78. String:key1, Value:false
  79. String:key2, value:44
  80. String:key3, value:43.6
  81. String:key4, Value:str
  82. */
  83. }
  84. int wmain ()
  85. {
  86. //This example uses the Task::wait method to ensure that async operations complete before the app exits.
  87. //In the most apps, you typically don?t wait for async operations to complete.
  88. Wcout << L"calling Requestjsonvalueasync ..." << Endl;
  89. Requestjsonvalueasync (). Wait ();
  90. Wcout << L"calling Iteratejsonvalue ..." << Endl;
  91. Iteratejsonvalue ();
  92. GetChar ();
  93. }



Compile, run, result:

.............

D)/*]]>*/</script></body>Calling Iteratejsonvalue ...
String:key1, Value:false
String:key2, value:44
String:key3, value:43.600000000000001
String:key4, Value: "Str"

C + + REST SDK I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.