$ brew install cpprestsdk $ brew install boost $ brew install libressl
打開 Xcode,File / New / Project...
在嚮導的第1頁選 macOS / Command Line Tool
在嚮導的第2頁語言選 C++,Product Name 填上任意名稱
在嚮導的第3頁選擇任意文件夾,點擊 Create 建立工程。html
將 System Header Search Paths 設置爲
/usr/local/Cellar/cpprestsdk/2.10.2/include
/usr/local/Cellar/boost/1.67.0_1/include
/usr/local/Cellar/libressl/2.7.4/includegit
將 Library Search Paths 設置爲
/usr/local/Cellar/cpprestsdk/2.10.2/lib
/usr/local/Cellar/boost/1.67.0_1/lib
/usr/local/Cellar/libressl/2.7.4/libgithub
將 Other Linker Flags 設置爲
-lcpprest -lboost_system -lboost_thread-mt -lboost_chrono-mt -lssl -lcryptoweb
cpprestsdk: Undefined symbols for architecture x86_64json
#include <cpprest/http_client.h> #include <cpprest/filestream.h> 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 int main(int argc, char* argv[]) { auto fileStream = std::make_shared<ostream>(); // Open stream to output file. pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile) { *fileStream = outFile; // Create http_client to send the request. http_client client(U("http://www.bing.com/")); // Build request URI and start the request. uri_builder builder(U("/search")); builder.append_query(U("q"), U("cpprestsdk github")); 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()); } return 0; }
這段代碼訪問
http://www.bing.com/search?q=cpprestsdk%20github
將結果保存爲 results.html。app
JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一個用於測試的 REST API 網站。
如下使用 RxJava2 + Retrofit2 調用該網站的 REST API,獲取字符串以及 JSON 數據。async
全部 GET API 都返回JSON數據,格式(JSON-Schema)以下:ide
{ "type":"object", "properties": { "userId": {"type" : "integer"}, "id": {"type" : "integer"}, "title": {"type" : "string"}, "body": {"type" : "string"} } }
#include <cpprest/http_client.h> #include <cpprest/filestream.h> #include <cpprest/json.h> #include <boost/algorithm/string/replace.hpp> 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 std; static void print_results(json::value const & value) { if(!value.is_null()) { auto userId = value.at(U("userId")).as_integer(); auto id = value.at(U("id")).as_integer(); auto title = value.at(U("title")).as_string(); auto body = boost::algorithm::replace_all_copy(value.at(U("body")).as_string(), "\n", "\\n"); cout << "Post {userId = " << userId << ", id = " << id << ", title = \"" << title << "\", body = \"" << body << "\"}" << endl; } } static void json_get() { http_client client(U("https://jsonplaceholder.typicode.com/")); // Build request URI and start the request. uri_builder builder(U("posts/1")); client // send the HTTP GET request asynchronous .request(methods::GET, builder.to_string()) // continue when the response is available .then([](http_response response) -> pplx::task<json::value> { // if the status is OK extract the body of the response into a JSON value // works only when the content type is application\json if(response.status_code() == status_codes::OK) { return response.extract_json(); } // return an empty JSON value return pplx::task_from_result(json::value()); }) // continue when the JSON value is available .then([](pplx::task<json::value> previousTask) { // get the JSON value from the task and display content from it try { json::value const & v = previousTask.get(); print_results(v); } catch (http_exception const & e) { printf("Error exception:%s\n", e.what()); } }) .wait(); } static void json_post() { http_client client(U("https://jsonplaceholder.typicode.com/")); json::value json_v ; json_v["userId"] = json::value::number(101); json_v["title"] = json::value::string("test title"); json_v["body"] = json::value::string("test body"); client .request(methods::POST, U("posts"), json_v) .then([](http_response response) -> pplx::task<string_t> { if(response.status_code() == status_codes::Created) { return response.extract_string(); } return pplx::task_from_result(string_t()); }) .then([](pplx::task<string_t> previousTask) { try { string_t const & v = previousTask.get(); cout << v << endl; } catch (http_exception const & e) { printf("Error exception:%s\n", e.what()); } }) .wait(); } static void json_update() { http_client client(U("https://jsonplaceholder.typicode.com/")); json::value json_v ; json_v["userId"] = json::value::number(101); json_v["title"] = json::value::string("test title"); json_v["body"] = json::value::string("test body"); client .request(methods::PUT, U("posts/1"), json_v) .then([](http_response response) -> pplx::task<string_t> { if(response.status_code() == status_codes::OK) { return response.extract_string(); } return pplx::task_from_result(string_t()); }) .then([](pplx::task<string_t> previousTask) { try { string_t const & v = previousTask.get(); cout << v << endl; } catch (http_exception const & e) { printf("Error exception:%s\n", e.what()); } }) .wait(); } static void json_delete() { http_client client(U("https://jsonplaceholder.typicode.com/")); client .request(methods::DEL, U("posts/1")) .then([](http_response response) -> pplx::task<string_t> { if(response.status_code() == status_codes::OK) { return response.extract_string(); } return pplx::task_from_result(string_t()); }) .then([](pplx::task<string_t> previousTask) { try { string_t const & v = previousTask.get(); cout << v << endl; } catch (http_exception const & e) { printf("Error exception:%s\n", e.what()); } }) .wait(); } int main(int argc, char* argv[]) { json_get(); json_post(); json_update(); json_delete(); return 0; }
這段代碼調用4個 REST API
而後打印所返回的 JSON 數據以及字符串的內容post
Post {userId = 1, id = 1, title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body = "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"} { "body": "test body", "title": "test title", "userId": 101, "id": 101 } { "body": "test body", "title": "test title", "userId": 101, "id": 1 } {}