Skip to content

Commit 57a3fb7

Browse files
author
Levent KARAGÖL
committed
setUserAgent method has been added
1 parent 54eb66b commit 57a3fb7

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

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 TLS version to be used for the request with setTLSVersion 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)