Skip to content

Set user agent #31

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 4 commits into from
Apr 27, 2024
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
54 changes: 47 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <fstream>
#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.
Expand Down Expand Up @@ -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<HttpResult> 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<HttpResult> send() noexcept
```


Expand Down
17 changes: 17 additions & 0 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -260,6 +275,8 @@ int main()

setTLSVersion();

setUserAgent();

setTimeout();

setDownloadAndUploadBandwidthLimit();
Expand Down
54 changes: 36 additions & 18 deletions src/libcpp-http-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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<std::string, std::string> headers;
Expand Down Expand Up @@ -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());
Expand Down
20 changes: 20 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading