diff --git a/README.md b/README.md index fd36337..5eb8527 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,21 @@ Inspired by [Sinatra](http://www.sinatrarb.com/) svr.run(); } +Client Example +-------------- + + #include + #include + using namespace httplib; + + int main(void) + { + Client cli("localhost", 1234); + + auto res = cli.get("/hi"); + if (res && res->status == 200) { + std::cout << res->body << std::endl; + } + } + Copyright (c) 2012 Yuji Hirose. All rights reserved. diff --git a/example/client.cc b/example/client.cc index 5378749..eb06b9b 100644 --- a/example/client.cc +++ b/example/client.cc @@ -17,11 +17,11 @@ int main(void) Client cli("localhost", 8080); - Response res; - if (cli.get(hi, res)) { - cout << res.status << endl; - cout << res.get_header_value("Content-Type") << endl; - cout << res.body << endl; + auto res = cli.get(hi); + if (res) { + cout << res->status << endl; + cout << res->get_header_value("Content-Type") << endl; + cout << res->body << endl; } return 0; diff --git a/httplib.h b/httplib.h index 6e28505..bf988c0 100644 --- a/httplib.h +++ b/httplib.h @@ -126,6 +126,9 @@ public: bool post(const char* url, const std::string& body, const char* content_type, Response& res); bool send(const Request& req, Response& res); + std::shared_ptr get(const char* url); + std::shared_ptr post(const char* url, const std::string& body, const char* content_type); + private: bool read_response_line(FILE* fp, Response& res); @@ -634,7 +637,8 @@ inline bool Client::get(const char* url, Response& res) return send(req, res); } -inline bool Client::post(const char* url, const std::string& body, const char* content_type, Response& res) +inline bool Client::post( + const char* url, const std::string& body, const char* content_type, Response& res) { Request req; req.method = "POST"; @@ -670,6 +674,19 @@ inline bool Client::send(const Request& req, Response& res) return true; } +inline std::shared_ptr Client::get(const char* url) +{ + auto res = std::make_shared(); + return get(url, *res) ? res : nullptr; +} + +inline std::shared_ptr Client::post( + const char* url, const std::string& body, const char* content_type) +{ + auto res = std::make_shared(); + return post(url, body, content_type, *res) ? res : nullptr; +} + } // namespace httplib #endif diff --git a/test/test.cc b/test/test.cc index a3351b2..41a3cb3 100644 --- a/test/test.cc +++ b/test/test.cc @@ -187,4 +187,36 @@ TEST_F(ServerTest, PostMethod) } } +TEST_F(ServerTest, GetMethod200Shared) +{ + auto res = Client(host_, port_).get("/hi"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); + EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); + EXPECT_EQ("Hello World!", res->body); +} + +TEST_F(ServerTest, PostMethodShared) +{ + { + auto res = Client(host_, port_).get("/person/john3"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(404, res->status); + } + { + auto content = "name=john3¬e=coder"; + auto content_type = "application/x-www-form-urlencoded"; + auto res = Client(host_, port_).post("/person", content, content_type); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + } + { + auto res = Client(host_, port_).get("/person/john3"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ("text/plain", res->get_header_value("Content-Type")); + ASSERT_EQ("coder", res->body); + } +} + // vim: et ts=4 sw=4 cin cino={1s ff=unix