Skip to content

Commit 5542bc9

Browse files
Merge pull request #31 from lk-libs/set-user-agent
Set user agent
2 parents 54eb66b + 53d22fd commit 5542bc9

File tree

4 files changed

+120
-25
lines changed

4 files changed

+120
-25
lines changed

README.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Modern, non-blocking and exception free, header-only HTTP Client library for C++
2626
* [How to ignore SSL certificate errors?](#how-to-ignore-ssl-certificate-errors)
2727
* [Setting the TLS version](#setting-the-tls-version)
2828
* [How to set timeout?](#how-to-set-timeout)
29+
* [Setting the User Agent](#setting-the-user-agent)
2930
* [How can I limit download and upload bandwidth?](#how-can-i-limit-download-and-upload-bandwidth)
3031
* [Semantic Versioning](#semantic-versioning)
3132
* [Full function list](#full-function-list)
@@ -438,6 +439,30 @@ int main() {
438439
```
439440

440441

442+
## Setting the User Agent
443+
444+
You can set the User Agent information to be sent during the request with the setUserAgent method.
445+
446+
```cpp
447+
#include <fstream>
448+
#include "libcpp-http-client.hpp"
449+
450+
using namespace lklibs;
451+
452+
int main() {
453+
HttpRequest httpRequest("https://api.myproject.com");
454+
455+
// You can set the user agent to be used for the request with setUserAgent method
456+
auto response = httpRequest
457+
.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")
458+
.send()
459+
.get();
460+
461+
return 0;
462+
}
463+
```
464+
465+
441466
## How can I limit download and upload bandwidth?
442467

443468
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
488513
> All methods and parameters descriptions are also available within the code as comment for IDEs.
489514
490515
```cpp
491-
- HttpRequest &setMethod(const HttpMethod &method) noexcept
492-
- HttpRequest &setQueryString(const std::string &queryString) noexcept
493-
- HttpRequest &setPayload(const std::string &payload) noexcept
494-
- HttpRequest &returnAsBinary() noexcept
495-
- HttpRequest &ignoreSslErrors() noexcept
496-
- HttpRequest &addHeader(const std::string &key, const std::string &value) noexcept
497-
- std::future<HttpResult> send() noexcept
516+
- HttpRequest& setMethod(const HttpMethod& method) noexcept
517+
518+
- HttpRequest& setQueryString(const std::string& queryString) noexcept
519+
520+
- HttpRequest& setPayload(const std::string& payload) noexcept
521+
522+
- HttpRequest& returnAsBinary() noexcept
498523

524+
- HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept
525+
526+
- HttpRequest& setTimeout(const int timeout) noexcept
527+
528+
- HttpRequest& ignoreSslErrors() noexcept
529+
530+
- HttpRequest& setTLSVersion(const TLSVersion version) noexcept
531+
532+
- HttpRequest& setUserAgent(const std::string& userAgent) noexcept
533+
534+
- HttpRequest& setDownloadBandwidthLimit(const int limit) noexcept
535+
536+
- HttpRequest& setUploadBandwidthLimit(const int limit) noexcept
537+
538+
- std::future<HttpResult> send() noexcept
499539
```
500540
501541

examples/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ void setTLSVersion()
202202
std::cout << "Data: " << response.textData << std::endl;
203203
}
204204

205+
void setUserAgent()
206+
{
207+
HttpRequest httpRequest("https://httpbun.com/get");
208+
209+
// You can set the user agent to be used for the request with setUserAgent method
210+
auto response = httpRequest
211+
.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")
212+
.send()
213+
.get();
214+
215+
std::cout << "Succeed: " << response.succeed << std::endl;
216+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
217+
std::cout << "Data: " << response.textData << std::endl;
218+
}
219+
205220
void setTimeout()
206221
{
207222
HttpRequest httpRequest("https://httpstat.us/504?sleep=10000");
@@ -260,6 +275,8 @@ int main()
260275

261276
setTLSVersion();
262277

278+
setUserAgent();
279+
263280
setTimeout();
264281

265282
setDownloadAndUploadBandwidthLimit();

src/libcpp-http-client.hpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -221,48 +221,60 @@ namespace lklibs
221221
}
222222

223223
/**
224-
* @brief Ignore SSL errors when making HTTP requests
224+
* @brief Add a HTTP header to the request
225+
*
226+
* @param key: Header key
227+
* @param value: Header value
225228
*/
226-
HttpRequest& ignoreSslErrors() noexcept
229+
HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept
227230
{
228-
this->sslErrorsWillBeIgnored = true;
231+
this->headers[key] = value;
229232

230233
return *this;
231234
}
232235

233236
/**
234-
* @brief Set the TLS version for the request
237+
* @brief Set the timeout for the request
235238
*
236-
* @param version: TLS version to be used for the request
239+
* @param timeout: Timeout in seconds (0 for no timeout)
237240
*/
238-
HttpRequest& setTLSVersion(TLSVersion version) noexcept
241+
HttpRequest& setTimeout(const int timeout) noexcept
239242
{
240-
this->tlsVersion = version;
243+
this->timeout = timeout;
241244

242245
return *this;
243246
}
244247

245248
/**
246-
* @brief Add a HTTP header to the request
249+
* @brief Ignore SSL errors when making HTTP requests
250+
*/
251+
HttpRequest& ignoreSslErrors() noexcept
252+
{
253+
this->sslErrorsWillBeIgnored = true;
254+
255+
return *this;
256+
}
257+
258+
/**
259+
* @brief Set the TLS version for the request
247260
*
248-
* @param key: Header key
249-
* @param value: Header value
261+
* @param version: TLS version to be used for the request
250262
*/
251-
HttpRequest& addHeader(const std::string& key, const std::string& value) noexcept
263+
HttpRequest& setTLSVersion(const TLSVersion version) noexcept
252264
{
253-
this->headers[key] = value;
265+
this->tlsVersion = version;
254266

255267
return *this;
256268
}
257269

258270
/**
259-
* @brief Set the timeout for the request
271+
* @brief Set the user agent for the request
260272
*
261-
* @param timeout: Timeout in seconds (0 for no timeout)
273+
* @param userAgent: User agent to be used for the request
262274
*/
263-
HttpRequest& setTimeout(int timeout) noexcept
275+
HttpRequest& setUserAgent(const std::string& userAgent) noexcept
264276
{
265-
this->timeout = timeout;
277+
this->userAgent = userAgent;
266278

267279
return *this;
268280
}
@@ -272,7 +284,7 @@ namespace lklibs
272284
*
273285
* @param limit: Download bandwidth limit in bytes per second (0 for no limit)
274286
*/
275-
HttpRequest& setDownloadBandwidthLimit(int limit) noexcept
287+
HttpRequest& setDownloadBandwidthLimit(const int limit) noexcept
276288
{
277289
this->downloadBandwidthLimit = limit;
278290

@@ -284,7 +296,7 @@ namespace lklibs
284296
*
285297
* @param limit: Upload bandwidth limit in bytes per second (0 for no limit)
286298
*/
287-
HttpRequest& setUploadBandwidthLimit(int limit) noexcept
299+
HttpRequest& setUploadBandwidthLimit(const int limit) noexcept
288300
{
289301
this->uploadBandwidthLimit = limit;
290302

@@ -322,6 +334,7 @@ namespace lklibs
322334
std::string url;
323335
std::string method = "GET";
324336
std::string payload;
337+
std::string userAgent;
325338
bool sslErrorsWillBeIgnored = false;
326339
ReturnFormat returnFormat = ReturnFormat::TEXT;
327340
std::map<std::string, std::string> headers;
@@ -386,6 +399,11 @@ namespace lklibs
386399
curl_easy_setopt(curl.get(), CURLOPT_MAX_SEND_SPEED_LARGE, this->uploadBandwidthLimit);
387400
curl_easy_setopt(curl.get(), CURLOPT_MAX_RECV_SPEED_LARGE, this->downloadBandwidthLimit);
388401

402+
if (!this->userAgent.empty())
403+
{
404+
curl_easy_setopt(curl.get(), CURLOPT_USERAGENT, this->userAgent.c_str());
405+
}
406+
389407
if (!this->payload.empty())
390408
{
391409
curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, this->payload.c_str());

test/test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,26 @@ TEST(TimeoutTest, TimeoutCanBeSet)
773773
ASSERT_FALSE(response.errorMessage.empty()) << "HTTP Error Message is empty";
774774
}
775775

776+
TEST(UserAgentTest, UserAgentCanBeSet)
777+
{
778+
HttpRequest httpRequest("https://httpbun.com/get");
779+
780+
auto response = httpRequest
781+
.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")
782+
.send()
783+
.get();
784+
785+
ASSERT_TRUE(response.succeed) << "HTTP Request failed";
786+
ASSERT_EQ(response.statusCode, 200) << "HTTP Status Code is not 200";
787+
ASSERT_FALSE(response.textData.empty()) << "HTTP Response is empty";
788+
ASSERT_TRUE(response.binaryData.empty()) << "Binary data is not empty";
789+
ASSERT_TRUE(response.errorMessage.empty()) << "HTTP Error Message is not empty";
790+
791+
auto data = json::parse(response.textData);
792+
793+
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";
794+
}
795+
776796
TEST(BandwidthLimitTest, DownloadBandwidthLimitCanBeSet)
777797
{
778798
HttpRequest httpRequest("https://httpbun.com/get");

0 commit comments

Comments
 (0)