Skip to content

Fix #57 and #62 #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ int main(void)

Server svr;

svr.get("/hi", [](const Request& req, Response& res) {
svr.Get("/hi", [](const Request& req, Response& res) {
res.set_content("Hello World!", "text/plain");
});

svr.get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
auto numbers = req.matches[1];
res.set_content(numbers, "text/plain");
});
Expand All @@ -32,13 +32,15 @@ int main(void)
}
```

`Post`, `Put`, `Delete` and `Options` methods are also supported.

### Method Chain

```cpp
svr.get("/get", [](const auto& req, auto& res) {
svr.Get("/get", [](const auto& req, auto& res) {
res.set_content("get", "text/plain");
})
.post("/post", [](const auto& req, auto& res) {
.Post("/post", [](const auto& req, auto& res) {
res.set_content(req.body(), "text/plain");
})
.listen("localhost", 1234);
Expand Down Expand Up @@ -72,7 +74,7 @@ svr.set_error_handler([](const auto& req, auto& res) {
### 'multipart/form-data' POST data

```cpp
svr.post("/multipart", [&](const auto& req, auto& res) {
svr.Post("/multipart", [&](const auto& req, auto& res) {
auto size = req.files.size();
auto ret = req.has_file("name1"));
const auto& file = req.get_file_value("name1");
Expand All @@ -95,7 +97,7 @@ int main(void)
{
httplib::Client cli("localhost", 1234);

auto res = cli.get("/hi");
auto res = cli.Get("/hi");
if (res && res->status == 200) {
std::cout << res->body << std::endl;
}
Expand All @@ -105,8 +107,8 @@ int main(void)
### POST

```c++
res = cli.post("/post", "text", "text/plain");
res = cli.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
res = cli.Post("/post", "text", "text/plain");
res = cli.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
```

### POST with parameters
Expand All @@ -115,7 +117,26 @@ res = cli.post("/person", "name=john1&note=coder", "application/x-www-form-urlen
httplib::Map params;
params["name"] = "john";
params["note"] = "coder";
auto res = cli.post("/post", params);
auto res = cli.Post("/post", params);
```

### PUT

```c++
res = cli.Post("/resource/foo", "text", "text/plain");
```

### DELETE

```c++
res = cli.Delete("/resource/foo");
```

### OPTIONS

```c++
res = cli.Options("*");
res = cli.Options("/resource/foo");
```

### Connection Timeout
Expand All @@ -130,7 +151,7 @@ httplib::Client client(url, port);

// prints: 0 / 000 bytes => 50% complete
std::shared_ptr<httplib::Response> res =
cli.get("/", [](uint64_t len, uint64_t total) {
cli.Get("/", [](uint64_t len, uint64_t total) {
printf("%lld / %lld bytes => %d%% complete\n",
len, total,
(int)((len/total)*100));
Expand All @@ -150,7 +171,7 @@ httplib::Client cli("httpbin.org", 80);
// 'Range: bytes=1-10'
httplib::Headers headers = { httplib::make_range_header(1, 10) };

auto res = cli.get("/range/32", headers);
auto res = cli.Get("/range/32", headers);
// res->status should be 206.
// res->body should be "bcdefghijk".
```
Expand Down Expand Up @@ -185,4 +206,4 @@ The server applies gzip compression to the following MIME type contents:
License
-------

MIT license (© 2017 Yuji Hirose)
MIT license (© 2018 Yuji Hirose)
2 changes: 1 addition & 1 deletion example/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(void) {

for (int i = 0; i < 3; i++) {
StopWatch sw(to_string(i).c_str());
auto res = cli.post("/post", body, "application/octet-stream");
auto res = cli.Post("/post", body, "application/octet-stream");
assert(res->status == 200);
}

Expand Down
2 changes: 1 addition & 1 deletion example/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(void)
httplib::Client cli("localhost", 8080);
#endif

auto res = cli.get("/hi");
auto res = cli.Get("/hi");
if (res) {
cout << res->status << endl;
cout << res->get_header_value("Content-Type") << endl;
Expand Down
2 changes: 1 addition & 1 deletion example/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(void)
{
Server svr;

svr.get("/hi", [](const auto& /*req*/, auto& res) {
svr.Get("/hi", [](const auto& /*req*/, auto& res) {
res.set_content("Hello World!", "text/plain");
});

Expand Down
10 changes: 5 additions & 5 deletions example/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ int main(void)
return -1;
}

svr.get("/", [=](const auto& /*req*/, auto& res) {
svr.Get("/", [=](const auto& /*req*/, auto& res) {
res.set_redirect("/hi");
});

svr.get("/hi", [](const auto& /*req*/, auto& res) {
svr.Get("/hi", [](const auto& /*req*/, auto& res) {
res.set_content("Hello World!\n", "text/plain");
});

svr.get("/slow", [](const auto& /*req*/, auto& res) {
svr.Get("/slow", [](const auto& /*req*/, auto& res) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(2s);
res.set_content("Slow...\n", "text/plain");
});

svr.get("/dump", [](const auto& req, auto& res) {
svr.Get("/dump", [](const auto& req, auto& res) {
res.set_content(dump_headers(req.headers), "text/plain");
});

svr.get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
svr.Get("/stop", [&](const auto& /*req*/, auto& /*res*/) {
svr.stop();
});

Expand Down
2 changes: 1 addition & 1 deletion example/simplesvr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int main(int argc, const char** argv)
Server svr(version);
#endif

svr.post("/multipart", [](const auto& req, auto& res) {
svr.Post("/multipart", [](const auto& req, auto& res) {
auto body =
dump_headers(req.headers) +
dump_multipart_files(req.files);
Expand Down
Loading