From 7eb946f62eac4c37a32f26f6ad2bef49ec34df54 Mon Sep 17 00:00:00 2001 From: t11s Date: Thu, 8 Jan 2026 10:49:49 -0800 Subject: [PATCH 1/2] default to none for PostRedirectFlags matches libcurl default https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html --- include/cpr/redirect.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/cpr/redirect.h b/include/cpr/redirect.h index d4c8e576b..c3f24d3ad 100644 --- a/include/cpr/redirect.h +++ b/include/cpr/redirect.h @@ -21,12 +21,12 @@ enum class PostRedirectFlags : uint8_t { **/ POST_303 = 0x1 << 2, /** - * Default value. * Convenience option to enable all flags. * Same as CURL_REDIR_POST_ALL (https://curl.se/libcurl/c/CURLOPT_POSTREDIR.html). **/ POST_ALL = POST_301 | POST_302 | POST_303, /** + * Default value. * * Convenience option to disable all flags. **/ NONE = 0x0 @@ -66,9 +66,9 @@ class Redirect { bool cont_send_cred{false}; /** * Flags to control how to act after a redirect for a post request. - * Default: POST_ALL + * Default: NONE (matches libcurl) **/ - PostRedirectFlags post_flags{PostRedirectFlags::POST_ALL}; + PostRedirectFlags post_flags{PostRedirectFlags::NONE}; Redirect() = default; // NOLINTNEXTLINE (google-runtime-int) From 49a2107f290fefc62a541674f45e14e4d30e3647 Mon Sep 17 00:00:00 2001 From: t11s Date: Sat, 31 Jan 2026 01:53:24 -0500 Subject: [PATCH 2/2] fix: post tests --- test/post_tests.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test/post_tests.cpp b/test/post_tests.cpp index b87170d77..fe2b6f555 100644 --- a/test/post_tests.cpp +++ b/test/post_tests.cpp @@ -718,7 +718,7 @@ TEST(UrlEncodedPostTests, PostBodyWithBuffer) { TEST(PostRedirectTests, TempRedirectTest) { Url url{server->GetBaseUrl() + "/temporary_redirect.html"}; - Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}); + Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::POST_ALL)); std::string expected_text{ "{\n" " \"x\": 5\n" @@ -740,7 +740,7 @@ TEST(PostRedirectTests, TempRedirectNoneTest) { TEST(PostRedirectTests, PermRedirectTest) { Url url{server->GetBaseUrl() + "/permanent_redirect.html"}; - Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}); + Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::POST_ALL)); std::string expected_text{ "{\n" " \"x\": 5\n" @@ -760,6 +760,24 @@ TEST(PostRedirectTests, PermRedirectNoneTest) { EXPECT_EQ(ErrorCode::OK, response.error.code); } +TEST(PostRedirectTests, TempRedirectDefaultTest) { + Url url{server->GetBaseUrl() + "/temporary_redirect.html"}; + // Default PostRedirectFlags is NONE, so POST should not be preserved on redirect + Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}); + EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html"); + EXPECT_EQ(405, response.status_code); + EXPECT_EQ(ErrorCode::OK, response.error.code); +} + +TEST(PostRedirectTests, PermRedirectDefaultTest) { + Url url{server->GetBaseUrl() + "/permanent_redirect.html"}; + // Default PostRedirectFlags is NONE, so POST should not be preserved on redirect + Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}); + EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html"); + EXPECT_EQ(405, response.status_code); + EXPECT_EQ(ErrorCode::OK, response.error.code); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); ::testing::AddGlobalTestEnvironment(server);