diff --git a/cpr/curl_container.cpp b/cpr/curl_container.cpp index e4f8fc2c4..7576d3e22 100644 --- a/cpr/curl_container.cpp +++ b/cpr/curl_container.cpp @@ -40,6 +40,25 @@ const std::string CurlContainer::GetContent(const CurlHolder& holder) return content; } +template <> +const std::string CurlContainer::GetContent() const { + std::string content{}; + for (const Parameter& parameter : containerList_) { + if (!content.empty()) { + content += "&"; + } + + if (parameter.value.empty()) { + content += parameter.key; + } else { + content += parameter.key + "="; + content += parameter.value; + } + } + + return content; +} + template <> const std::string CurlContainer::GetContent(const CurlHolder& holder) const { std::string content{}; @@ -54,6 +73,19 @@ const std::string CurlContainer::GetContent(const CurlHolder& holder) cons return content; } +template <> +const std::string CurlContainer::GetContent() const { + std::string content{}; + for (const cpr::Pair& element : containerList_) { + if (!content.empty()) { + content += "&"; + } + content += element.key + "=" + element.value; + } + + return content; +} + template class CurlContainer; template class CurlContainer; diff --git a/include/cpr/curl_container.h b/include/cpr/curl_container.h index c2409b226..3f80586a4 100644 --- a/include/cpr/curl_container.h +++ b/include/cpr/curl_container.h @@ -40,8 +40,18 @@ class CurlContainer { void Add(const std::initializer_list&); void Add(const T&); + /** + * Returns the URL using curl_easy_escape(...) for escaping the given parameters. + * Requires `CurlHolder`. + **/ const std::string GetContent(const CurlHolder&) const; + /** + * Returns the URL while ignoring `encode`. This allows calling without + * active `CurlHolder`. + **/ + const std::string GetContent() const; + protected: std::vector containerList_; }; diff --git a/test/structures_tests.cpp b/test/structures_tests.cpp index 552fd1af1..d374e461b 100644 --- a/test/structures_tests.cpp +++ b/test/structures_tests.cpp @@ -50,6 +50,16 @@ TEST(ParametersTests, DisableEncodingTest) { EXPECT_EQ(parameters.GetContent(CurlHolder()), expected); } +TEST(ParametersTests, NoCurlHolderTest) { + std::string key1 = "key1"; + std::string key2 = "key2ยง$%&/"; + std::string value1 = "hello.,.,"; + std::string value2 = "hello"; + Parameters parameters{{key1, value1}, {key2, value2}}; + const std::string expected = key1 + '=' + value1 + '&' + key2 + '=' + value2; + EXPECT_EQ(parameters.GetContent(), expected); +} + TEST(UrlToAndFromString, UrlTests) { std::string s{"https://github.com/whoshuu/cpr"}; cpr::Url url = s;