diff --git a/httplib.h b/httplib.h index 3ea4529..ab1e529 100644 --- a/httplib.h +++ b/httplib.h @@ -604,6 +604,8 @@ public: std::shared_ptr Head(const char *path, const Headers &headers); + std::shared_ptr Post(const char *path); + std::shared_ptr Post(const char *path, const std::string &body, const char *content_type); @@ -631,6 +633,8 @@ public: std::shared_ptr Post(const char *path, const Headers &headers, const MultipartFormDataItems &items); + std::shared_ptr Put(const char *path); + std::shared_ptr Put(const char *path, const std::string &body, const char *content_type); @@ -831,7 +835,7 @@ inline void Post(std::vector &requests, const char *path, req.method = "POST"; req.path = path; req.headers = headers; - req.headers.emplace("Content-Type", content_type); + if (content_type) { req.headers.emplace("Content-Type", content_type); } req.body = body; requests.emplace_back(std::move(req)); } @@ -4030,7 +4034,7 @@ inline std::shared_ptr Client::send_with_content_provider( req.headers = headers; req.path = path; - req.headers.emplace("Content-Type", content_type); + if (content_type) { req.headers.emplace("Content-Type", content_type); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT if (compress_) { @@ -4222,6 +4226,10 @@ inline std::shared_ptr Client::Head(const char *path, return send(req, *res) ? res : nullptr; } +inline std::shared_ptr Client::Post(const char *path) { + return Post(path, std::string(), nullptr); +} + inline std::shared_ptr Client::Post(const char *path, const std::string &body, const char *content_type) { @@ -4294,6 +4302,10 @@ Client::Post(const char *path, const Headers &headers, return Post(path, headers, body, content_type.c_str()); } +inline std::shared_ptr Client::Put(const char *path) { + return Put(path, std::string(), nullptr); +} + inline std::shared_ptr Client::Put(const char *path, const std::string &body, const char *content_type) { diff --git a/test/test.cc b/test/test.cc index 689f524..c587e94 100644 --- a/test/test.cc +++ b/test/test.cc @@ -964,8 +964,24 @@ protected: .Post("/empty", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, ""); + EXPECT_EQ("text/plain", req.get_header_value("Content-Type")); + EXPECT_EQ("0", req.get_header_value("Content-Length")); res.set_content("empty", "text/plain"); }) + .Post("/empty-no-content-type", + [&](const Request &req, Response &res) { + EXPECT_EQ(req.body, ""); + EXPECT_FALSE(req.has_header("Content-Type")); + EXPECT_EQ("0", req.get_header_value("Content-Length")); + res.set_content("empty-no-content-type", "text/plain"); + }) + .Put("/empty-no-content-type", + [&](const Request &req, Response &res) { + EXPECT_EQ(req.body, ""); + EXPECT_FALSE(req.has_header("Content-Type")); + EXPECT_EQ("0", req.get_header_value("Content-Length")); + res.set_content("empty-no-content-type", "text/plain"); + }) .Put("/put", [&](const Request &req, Response &res) { EXPECT_EQ(req.body, "PUT"); @@ -1310,6 +1326,20 @@ TEST_F(ServerTest, PostEmptyContent) { ASSERT_EQ("empty", res->body); } +TEST_F(ServerTest, PostEmptyContentWithNoContentType) { + auto res = cli_.Post("/empty-no-content-type"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ("empty-no-content-type", res->body); +} + +TEST_F(ServerTest, PutEmptyContentWithNoContentType) { + auto res = cli_.Put("/empty-no-content-type"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ("empty-no-content-type", res->body); +} + TEST_F(ServerTest, GetMethodDir) { auto res = cli_.Get("/dir/"); ASSERT_TRUE(res != nullptr);