Skip to content
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
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ using namespace lklibs;

int main() {

HttpClient httpClient;

// The simplest but slowest method if multiple calls will be made
auto response = HttpClient::getRequest("https://api.myproject.com?param1=7&param2=test").get();
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test").get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -95,11 +97,13 @@ using namespace lklibs;

int main() {

auto response1 = HttpClient::getRequest("https://api.myproject.com/foo").get();
auto response2 = HttpClient::getRequest("https://api.myproject.com/bar").get();
auto response3 = HttpClient::getRequest("https://api.myproject.com/baz").get();
auto response4 = HttpClient::getRequest("https://api.myproject.com/qux").get();
auto response5 = HttpClient::getRequest("https://api.myproject.com/quux").get();
HttpClient httpClient;

auto response1 = httpClient.getRequest("https://api.myproject.com/foo").get();
auto response2 = httpClient.getRequest("https://api.myproject.com/bar").get();
auto response3 = httpClient.getRequest("https://api.myproject.com/baz").get();
auto response4 = httpClient.getRequest("https://api.myproject.com/qux").get();
auto response5 = httpClient.getRequest("https://api.myproject.com/quux").get();

// Takes 2.5 seconds in total

Expand All @@ -119,11 +123,11 @@ using namespace lklibs;

int main() {

auto future1 = HttpClient::getRequest("https://api.myproject.com/foo");
auto future2 = HttpClient::getRequest("https://api.myproject.com/bar");
auto future3 = HttpClient::getRequest("https://api.myproject.com/baz");
auto future4 = HttpClient::getRequest("https://api.myproject.com/qux");
auto future5 = HttpClient::getRequest("https://api.myproject.com/quux");
auto future1 = httpClient.getRequest("https://api.myproject.com/foo");
auto future2 = httpClient.getRequest("https://api.myproject.com/bar");
auto future3 = httpClient.getRequest("https://api.myproject.com/baz");
auto future4 = httpClient.getRequest("https://api.myproject.com/qux");
auto future5 = httpClient.getRequest("https://api.myproject.com/quux");

auto response1 = future1.get();
auto response2 = future2.get();
Expand Down Expand Up @@ -159,7 +163,7 @@ using namespace lklibs;

int main() {

auto response = HttpClient::getRequest("https://www.myinvalidurl.com").get();
auto response = httpClient.getRequest("https://www.myinvalidurl.com").get();

// Instead of throwing an exception, the succeed field of the response object is set to false
std::cout << "Succeed: " << response.succeed << std::endl;
Expand Down Expand Up @@ -192,7 +196,7 @@ using namespace lklibs;
int main() {

// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
auto response = HttpClient::getRequest("https://api.myproject.com/image/7", true).get();
auto response = httpClient.getRequest("https://api.myproject.com/image/7", true).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand Down Expand Up @@ -224,7 +228,7 @@ int main() {
headers["Custom-Header1"] = "value1";
headers["Custom-Header2"] = "value2";

auto response = HttpClient::getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();

std::cout << "Succeed: " << response.succeed << std::endl;

Expand All @@ -249,7 +253,7 @@ int main() {
// You can send a POST request with form data in the payload
std::string payload = "param1=7&param2=test";

auto response = HttpClient::postRequest("https://api.myproject.com", payload).get();
auto response = httpClient.postRequest("https://api.myproject.com", payload).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand Down Expand Up @@ -280,7 +284,7 @@ int main() {

headers["Content-Type"] = "application/json";

auto response = HttpClient::postRequest("https://api.myproject.com", payload, headers).get();
auto response = httpClient.postRequest("https://api.myproject.com", payload, headers).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -305,9 +309,9 @@ int main() {

std::string payload = "param1=7&param2=test";

auto future1 = HttpClient::putRequest("https://api.myproject.com", payload);
auto future2 = HttpClient::deleteRequest("https://api.myproject.com", payload);
auto future3 = HttpClient::patchRequest("https://api.myproject.com?param1=7&param2=test");
auto future1 = httpClient.putRequest("https://api.myproject.com", payload);
auto future2 = httpClient.deleteRequest("https://api.myproject.com", payload);
auto future3 = httpClient.patchRequest("https://api.myproject.com?param1=7&param2=test");

auto response1 = future1.get();
auto response2 = future2.get();
Expand Down
44 changes: 32 additions & 12 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ using namespace lklibs;

void simpleGet() {

HttpClient httpClient;

// The simplest but slowest method if multiple calls will be made
auto response = HttpClient::getRequest("https://httpbun.com/get?param1=7&param2=test").get();
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test").get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -15,10 +17,12 @@ void simpleGet() {

void nonBlockingGet() {

HttpClient httpClient;

// All requests are made one after the other without waiting for a response
auto future1 = HttpClient::getRequest("https://httpbun.com/get?param1=1&param2=test1");
auto future2 = HttpClient::getRequest("https://httpbun.com/get?param1=2&param2=test2");
auto future3 = HttpClient::getRequest("https://httpbun.com/get?param1=3&param2=test3");
auto future1 = httpClient.getRequest("https://httpbun.com/get?param1=1&param2=test1");
auto future2 = httpClient.getRequest("https://httpbun.com/get?param1=2&param2=test2");
auto future3 = httpClient.getRequest("https://httpbun.com/get?param1=3&param2=test3");

// Then all the answers are received. Thus, 3 requests are sent in parallel
auto response1 = future1.get();
Expand All @@ -40,8 +44,10 @@ void nonBlockingGet() {

void receiveBinaryData() {

HttpClient httpClient;

// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
auto response = HttpClient::getRequest("https://httpbun.com/bytes/100", true).get();
auto response = httpClient.getRequest("https://httpbun.com/bytes/100", true).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -52,8 +58,10 @@ void receiveBinaryData() {

void receiveError() {

HttpClient httpClient;

// This is an exception free library. If an error occurs, no exception is thrown
auto response = HttpClient::getRequest("https://httpbun.com/not_found").get();
auto response = httpClient.getRequest("https://httpbun.com/not_found").get();

// Instead, the succeed field of the response object is set to false
std::cout << "Succeed: " << response.succeed << std::endl;
Expand All @@ -67,23 +75,27 @@ void receiveError() {

void sendingHttpHeaders() {

HttpClient httpClient;

// You can send custom headers in a string/string map
auto headers = std::map<std::string, std::string>();

headers["Custom-Header1"] = "value1";
headers["Custom-Header2"] = "value2";

auto response = HttpClient::getRequest("https://httpbun.com/get?param1=7&param2=test", headers).get();
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test", headers).get();

std::cout << "Succeed: " << response.succeed << std::endl;
}

void simplePostWithFormData() {

HttpClient httpClient;

// You can send a POST request with form data in the payload
std::string payload = "param1=7&param2=test";

auto response = HttpClient::postRequest("https://httpbun.com/post", payload).get();
auto response = httpClient.postRequest("https://httpbun.com/post", payload).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -92,14 +104,16 @@ void simplePostWithFormData() {

void simplePostWithJSONData() {

HttpClient httpClient;

std::string payload = R"({"param1": 7, "param2": "test"})";

// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
auto headers = std::map<std::string, std::string>();

headers["Content-Type"] = "application/json";

auto response = HttpClient::postRequest("https://httpbun.com/post", payload, headers).get();
auto response = httpClient.postRequest("https://httpbun.com/post", payload, headers).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -108,10 +122,12 @@ void simplePostWithJSONData() {

void simplePutWithFormData() {

HttpClient httpClient;

// You can send a PUT request with form data in the payload just like POST
std::string payload = "param1=7&param2=test";

auto response = HttpClient::putRequest("https://httpbun.com/put", payload).get();
auto response = httpClient.putRequest("https://httpbun.com/put", payload).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -120,10 +136,12 @@ void simplePutWithFormData() {

void simpleDeleteWithFormData() {

HttpClient httpClient;

// You can send a DELETE request with form data in the payload just like POST
std::string payload = "param1=7&param2=test";

auto response = HttpClient::deleteRequest("https://httpbun.com/delete", payload).get();
auto response = httpClient.deleteRequest("https://httpbun.com/delete", payload).get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand All @@ -132,8 +150,10 @@ void simpleDeleteWithFormData() {

void simplePatch() {

HttpClient httpClient;

// You can send a PATCH request with QueryString just like GET
auto response = HttpClient::patchRequest("https://httpbun.com/patch?param1=7&param2=test").get();
auto response = httpClient.patchRequest("https://httpbun.com/patch?param1=7&param2=test").get();

std::cout << "Succeed: " << response.succeed << std::endl;
std::cout << "Http Status Code: " << response.statusCode << std::endl;
Expand Down
Loading