Skip to content

Commit 68044a4

Browse files
committed
curl_container: allow calling GetContent without CurlHolder
This can be relevant when the parameters need to be passed to some different library (i.e. libmpv, gstreamer) and there is no cpr::Session to get a CurlHolder from. Signed-off-by: Sebastian Muxel <sebastian@muxel.dev>
1 parent a0ff651 commit 68044a4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cpr/curl_container.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ const std::string CurlContainer<Parameter>::GetContent(const CurlHolder& holder)
4040
return content;
4141
}
4242

43+
template <>
44+
const std::string CurlContainer<Parameter>::GetContent() const {
45+
std::string content{};
46+
for (const Parameter& parameter : containerList_) {
47+
if (!content.empty()) {
48+
content += "&";
49+
}
50+
51+
if (parameter.value.empty()) {
52+
content += parameter.key;
53+
} else {
54+
content += parameter.key + "=";
55+
content += parameter.value;
56+
}
57+
}
58+
59+
return content;
60+
}
61+
4362
template <>
4463
const std::string CurlContainer<Pair>::GetContent(const CurlHolder& holder) const {
4564
std::string content{};
@@ -54,6 +73,19 @@ const std::string CurlContainer<Pair>::GetContent(const CurlHolder& holder) cons
5473
return content;
5574
}
5675

76+
template <>
77+
const std::string CurlContainer<Pair>::GetContent() const {
78+
std::string content{};
79+
for (const cpr::Pair& element : containerList_) {
80+
if (!content.empty()) {
81+
content += "&";
82+
}
83+
content += element.key + "=" + element.value;
84+
}
85+
86+
return content;
87+
}
88+
5789
template class CurlContainer<Pair>;
5890
template class CurlContainer<Parameter>;
5991

include/cpr/curl_container.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class CurlContainer {
4242

4343
const std::string GetContent(const CurlHolder&) const;
4444

45+
/**
46+
* Returns the URL while ignoring `encode`. This allows calling without
47+
* active `CurlHolder`.
48+
**/
49+
const std::string GetContent() const;
50+
4551
protected:
4652
std::vector<T> containerList_;
4753
};

0 commit comments

Comments
 (0)