Skip to content

Commit

Permalink
Add an example of a JSON request
Browse files Browse the repository at this point in the history
  • Loading branch information
elnormous committed Jul 19, 2021
1 parent 3ac0d81 commit b6b8795
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ try
{
http::Request request("http://test.com/test");
// pass parameters as a map
std::map<std::string, std::string> parameters = {{"foo", "1"}, {"bar", "baz"}};
const std::map<std::string, std::string> parameters = {{"foo", "1"}, {"bar", "baz"}};
const auto response = request.send("POST", parameters, {
"Content-Type: application/x-www-form-urlencoded"
});
Expand All @@ -61,6 +61,26 @@ catch (const std::exception& e)
}
```

Example of POST request with a JSON body
```cpp
#include "HTTPRequest.hpp"

try
{
http::Request request("http://test.com/test");
// pass parameters as a string
const std::string body = "{\"foo\": 1, \"bar\": \"baz\"}";
const auto response = request.send("POST", parameters, {
"Content-Type: application/json"
});
std::cout << std::string{response.body.begin(), response.body.end()} << '\n'; // print the result
}
catch (const std::exception& e)
{
std::cerr << "Request failed, error: " << e.what() << '\n';
}
```

To set a timeout for HTTP requests, pass `std::chrono::duration` as a last parameter to `send()`. A negative duration (default) passed to `send()` disables timeout.

## License
Expand Down

0 comments on commit b6b8795

Please sign in to comment.