Skip to content

Commit

Permalink
Extended the usage documentation, and added additional output in the …
Browse files Browse the repository at this point in the history
…example files, as suggested in openjournals/joss-reviews#1592 (comment)
  • Loading branch information
eidheim committed Jul 31, 2019
1 parent 5b6a606 commit 0eeb972
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ See also [benchmarks](https://gitlab.com/eidheim/Simple-Web-Server/blob/master/d

See [http_examples.cpp](https://gitlab.com/eidheim/Simple-Web-Server/blob/master/http_examples.cpp) or
[https_examples.cpp](https://gitlab.com/eidheim/Simple-Web-Server/blob/master/https_examples.cpp) for example usage.
Particularly, the JSON-POST (using Boost.PropertyTree) and the GET /match/[number] examples might be relevant.
The following server resources are setup using regular expressions to match request paths:
* `POST /string` - responds with the posted string.
* `POST /json` - parses the request content as JSON, and responds with some of the parsed values.
* `GET /info` - responds with information extracted from the request.
* `GET /match/([0-9]+)` - matches for instance `/match/123` and responds with the matched number `123`.
* `GET /work` - starts a thread, simulating heavy work, and responds when the work is done.
* `GET` - a special default_resource handler is called when a request path does not match any of the above resources.
This resource responds with the content of files in the `web/`-folder if the request path identifies one of these files.

[Documentation](https://eidheim.gitlab.io/Simple-Web-Server/annotated.html) is also available, generated from the master branch.

Expand Down
9 changes: 6 additions & 3 deletions http_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,23 @@ int main() {

// Synchronous request examples
try {
cout << "Example GET request to http://localhost:8080/match/123" << endl;
auto r1 = client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
cout << "Response content: " << r1->content.rdbuf() << endl << endl; // Alternatively, use the convenience function r1->content.string()

cout << "Example POST request to http://localhost:8080/string" << endl;
auto r2 = client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
cout << "Response content: " << r2->content.rdbuf() << endl << endl;
}
catch(const SimpleWeb::system_error &e) {
cerr << "Client request error: " << e.what() << endl;
}

// Asynchronous request example
cout << "Example POST request to http://localhost:8080/json" << endl;
client.request("POST", "/json", json_string, [](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
if(!ec)
cout << response->content.rdbuf() << endl;
cout << "Response content: " << response->content.rdbuf() << endl;
});
client.io_service->run();

Expand Down
9 changes: 6 additions & 3 deletions https_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,23 @@ int main() {

// Synchronous request examples
try {
cout << "Example GET request to https://localhost:8080/match/123" << endl;
auto r1 = client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
cout << "Response content: " << r1->content.rdbuf() << endl << endl; // Alternatively, use the convenience function r1->content.string()

cout << "Example POST request to https://localhost:8080/string" << endl;
auto r2 = client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
cout << "Response content: " << r2->content.rdbuf() << endl << endl;
}
catch(const SimpleWeb::system_error &e) {
cerr << "Client request error: " << e.what() << endl;
}

// Asynchronous request example
cout << "Example POST request to https://localhost:8080/json" << endl;
client.request("POST", "/json", json_string, [](shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) {
if(!ec)
cout << response->content.rdbuf() << endl;
cout << "Response content: " << response->content.rdbuf() << endl;
});
client.io_service->run();

Expand Down

0 comments on commit 0eeb972

Please sign in to comment.