Skip to content

Commit 66ee4ee

Browse files
committed
Add overloaded Get() functions to support query parameters and headers
1 parent 5a8fba4 commit 66ee4ee

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

httplib-stream.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ inline Result Get(Client &cli, const std::string &path,
322322
return Result{cli.open_stream(path, headers)};
323323
}
324324

325+
inline Result Get(Client &cli, const std::string &path, const Params &params) {
326+
return Result{cli.open_stream(append_query_params(path, params))};
327+
}
328+
329+
inline Result Get(Client &cli, const std::string &path, const Params &params,
330+
const Headers &headers) {
331+
return Result{cli.open_stream(append_query_params(path, params), headers)};
332+
}
333+
325334
} // namespace stream
326335

327336
} // namespace httplib

test/test-stream.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ class StreamingServerTest : public ::testing::Test {
5656
res.set_content("Hello World!", "text/plain");
5757
});
5858

59+
server_.Get("/echo-params",
60+
[](const httplib::Request &req, httplib::Response &res) {
61+
std::string result;
62+
for (const auto &p : req.params) {
63+
if (!result.empty()) result += "&";
64+
result += p.first + "=" + p.second;
65+
}
66+
res.set_content(result, "text/plain");
67+
});
68+
5969
server_.Get(
6070
"/chunked", [](const httplib::Request &, httplib::Response &res) {
6171
res.set_chunked_content_provider(
@@ -280,6 +290,34 @@ TEST_F(StreamingServerTest, stream_Get_404) {
280290
EXPECT_EQ(404, result.status());
281291
}
282292

293+
TEST_F(StreamingServerTest, stream_Get_WithParams) {
294+
httplib::Client cli("localhost", 8787);
295+
httplib::Params params = {{"foo", "bar"}, {"baz", "123"}};
296+
297+
auto result = httplib::stream::Get(cli, "/echo-params", params);
298+
299+
ASSERT_TRUE(result.is_valid());
300+
EXPECT_EQ(200, result.status());
301+
302+
std::string body = result.read_all();
303+
EXPECT_TRUE(body.find("foo=bar") != std::string::npos);
304+
EXPECT_TRUE(body.find("baz=123") != std::string::npos);
305+
}
306+
307+
TEST_F(StreamingServerTest, stream_Get_WithParamsAndHeaders) {
308+
httplib::Client cli("localhost", 8787);
309+
httplib::Params params = {{"key", "value"}};
310+
httplib::Headers headers = {{"X-Custom-Header", "test"}};
311+
312+
auto result = httplib::stream::Get(cli, "/echo-params", params, headers);
313+
314+
ASSERT_TRUE(result.is_valid());
315+
EXPECT_EQ(200, result.status());
316+
317+
std::string body = result.read_all();
318+
EXPECT_TRUE(body.find("key=value") != std::string::npos);
319+
}
320+
283321
TEST(GeneratorTest, EmptyGenerator) {
284322
auto gen = []() -> httplib::Generator<int> { co_return; }();
285323

0 commit comments

Comments
 (0)