diff --git a/README.md b/README.md index e9f9283..42ea179 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++ * [How to ignore SSL certificate errors?](#how-to-ignore-ssl-certificate-errors) * [Setting the TLS version](#setting-the-tls-version) * [How to set timeout?](#how-to-set-timeout) +* [Setting the User Agent](#setting-the-user-agent) * [How can I limit download and upload bandwidth?](#how-can-i-limit-download-and-upload-bandwidth) * [Semantic Versioning](#semantic-versioning) * [Full function list](#full-function-list) @@ -438,6 +439,30 @@ int main() { ``` +## Setting the User Agent + +You can set the User Agent information to be sent during the request with the setUserAgent method. + +```cpp +#include +#include "libcpp-http-client.hpp" + +using namespace lklibs; + +int main() { + HttpRequest httpRequest("https://api.myproject.com"); + + // You can set the user agent to be used for the request with setUserAgent method + auto response = httpRequest + .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0") + .send() + .get(); + + return 0; +} +``` + + ## How can I limit download and upload bandwidth? If you do not want the bandwidth to exceed a certain limit during the download and upload process, you can determine the maximum limit that can be used in Bytes with the setDownloadBandwidthLimit and setUploadBandwidthLimit methods. @@ -488,14 +513,29 @@ send return the class itself, so they can be added one after the other like a ch > All methods and parameters descriptions are also available within the code as comment for IDEs. ```cpp -- HttpRequest &setMethod(const HttpMethod &method) noexcept -- HttpRequest &setQueryString(const std::string &queryString) noexcept -- HttpRequest &setPayload(const std::string &payload) noexcept -- HttpRequest &returnAsBinary() noexcept -- HttpRequest &ignoreSslErrors() noexcept -- HttpRequest &addHeader(const std::string &key, const std::string &value) noexcept -- std::future send() noexcept +- HttpRequest& setMethod(const HttpMethod& method) noexcept + +- HttpRequest& setQueryString(const std::string& queryString) noexcept + +- HttpRequest& setPayload(const std::string& payload) noexcept + +- HttpRequest& returnAsBinary() noexcept +- HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept + +- HttpRequest& setTimeout(const int timeout) noexcept + +- HttpRequest& ignoreSslErrors() noexcept + +- HttpRequest& setTLSVersion(const TLSVersion version) noexcept + +- HttpRequest& setUserAgent(const std::string& userAgent) noexcept + +- HttpRequest& setDownloadBandwidthLimit(const int limit) noexcept + +- HttpRequest& setUploadBandwidthLimit(const int limit) noexcept + +- std::future send() noexcept ``` diff --git a/examples/main.cpp b/examples/main.cpp index dda014d..d39ef87 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -202,6 +202,21 @@ void setTLSVersion() std::cout << "Data: " << response.textData << std::endl; } +void setUserAgent() +{ + HttpRequest httpRequest("https://httpbun.com/get"); + + // You can set the user agent to be used for the request with setUserAgent method + auto response = httpRequest + .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0") + .send() + .get(); + + std::cout << "Succeed: " << response.succeed << std::endl; + std::cout << "Http Status Code: " << response.statusCode << std::endl; + std::cout << "Data: " << response.textData << std::endl; +} + void setTimeout() { HttpRequest httpRequest("https://httpstat.us/504?sleep=10000"); @@ -260,6 +275,8 @@ int main() setTLSVersion(); + setUserAgent(); + setTimeout(); setDownloadAndUploadBandwidthLimit(); diff --git a/src/libcpp-http-client.hpp b/src/libcpp-http-client.hpp index 50e4c5c..a981836 100644 --- a/src/libcpp-http-client.hpp +++ b/src/libcpp-http-client.hpp @@ -221,48 +221,60 @@ namespace lklibs } /** - * @brief Ignore SSL errors when making HTTP requests + * @brief Add a HTTP header to the request + * + * @param key: Header key + * @param value: Header value */ - HttpRequest& ignoreSslErrors() noexcept + HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept { - this->sslErrorsWillBeIgnored = true; + this->headers[key] = value; return *this; } /** - * @brief Set the TLS version for the request + * @brief Set the timeout for the request * - * @param version: TLS version to be used for the request + * @param timeout: Timeout in seconds (0 for no timeout) */ - HttpRequest& setTLSVersion(TLSVersion version) noexcept + HttpRequest& setTimeout(const int timeout) noexcept { - this->tlsVersion = version; + this->timeout = timeout; return *this; } /** - * @brief Add a HTTP header to the request + * @brief Ignore SSL errors when making HTTP requests + */ + HttpRequest& ignoreSslErrors() noexcept + { + this->sslErrorsWillBeIgnored = true; + + return *this; + } + + /** + * @brief Set the TLS version for the request * - * @param key: Header key - * @param value: Header value + * @param version: TLS version to be used for the request */ - HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept + HttpRequest& setTLSVersion(const TLSVersion version) noexcept { - this->headers[key] = value; + this->tlsVersion = version; return *this; } /** - * @brief Set the timeout for the request + * @brief Set the user agent for the request * - * @param timeout: Timeout in seconds (0 for no timeout) + * @param userAgent: User agent to be used for the request */ - HttpRequest& setTimeout(int timeout) noexcept + HttpRequest& setUserAgent(const std::string& userAgent) noexcept { - this->timeout = timeout; + this->userAgent = userAgent; return *this; } @@ -272,7 +284,7 @@ namespace lklibs * * @param limit: Download bandwidth limit in bytes per second (0 for no limit) */ - HttpRequest& setDownloadBandwidthLimit(int limit) noexcept + HttpRequest& setDownloadBandwidthLimit(const int limit) noexcept { this->downloadBandwidthLimit = limit; @@ -284,7 +296,7 @@ namespace lklibs * * @param limit: Upload bandwidth limit in bytes per second (0 for no limit) */ - HttpRequest& setUploadBandwidthLimit(int limit) noexcept + HttpRequest& setUploadBandwidthLimit(const int limit) noexcept { this->uploadBandwidthLimit = limit; @@ -322,6 +334,7 @@ namespace lklibs std::string url; std::string method = "GET"; std::string payload; + std::string userAgent; bool sslErrorsWillBeIgnored = false; ReturnFormat returnFormat = ReturnFormat::TEXT; std::map headers; @@ -386,6 +399,11 @@ namespace lklibs curl_easy_setopt(curl.get(), CURLOPT_MAX_SEND_SPEED_LARGE, this->uploadBandwidthLimit); curl_easy_setopt(curl.get(), CURLOPT_MAX_RECV_SPEED_LARGE, this->downloadBandwidthLimit); + if (!this->userAgent.empty()) + { + curl_easy_setopt(curl.get(), CURLOPT_USERAGENT, this->userAgent.c_str()); + } + if (!this->payload.empty()) { curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, this->payload.c_str()); diff --git a/test/test.cpp b/test/test.cpp index 6ed955f..a4333ca 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -773,6 +773,26 @@ TEST(TimeoutTest, TimeoutCanBeSet) ASSERT_FALSE(response.errorMessage.empty()) << "HTTP Error Message is empty"; } +TEST(UserAgentTest, UserAgentCanBeSet) +{ + HttpRequest httpRequest("https://httpbun.com/get"); + + auto response = httpRequest + .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0") + .send() + .get(); + + ASSERT_TRUE(response.succeed) << "HTTP Request failed"; + ASSERT_EQ(response.statusCode, 200) << "HTTP Status Code is not 200"; + ASSERT_FALSE(response.textData.empty()) << "HTTP Response is empty"; + ASSERT_TRUE(response.binaryData.empty()) << "Binary data is not empty"; + ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty"; + + auto data = json::parse(response.textData); + + ASSERT_EQ(data["headers"]["User-Agent"], "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0") << "User-Agent is invalid"; +} + TEST(BandwidthLimitTest, DownloadBandwidthLimitCanBeSet) { HttpRequest httpRequest("https://httpbun.com/get");