diff --git a/httplib.h b/httplib.h index e494b84..878f16d 100644 --- a/httplib.h +++ b/httplib.h @@ -2068,12 +2068,7 @@ inline void Client::write_request(Stream& strm, Request& req) // Body if (!req.body.empty()) { - if (req.get_header_value("Content-Type") == "application/x-www-form-urlencoded") { - auto str = detail::encode_url(req.body); - bstrm.write(str.c_str(), str.size()); - } else { - bstrm.write(req.body.c_str(), req.body.size()); - } + bstrm.write(req.body.c_str(), req.body.size()); } // Flush buffer diff --git a/test/test.cc b/test/test.cc index f29c7d3..5487db8 100644 --- a/test/test.cc +++ b/test/test.cc @@ -23,6 +23,8 @@ const int PORT = 1234; const string LONG_QUERY_VALUE = string(25000, '@'); const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE; +const std::string JSON_DATA = "{\"hello\":\"world\"}"; + #ifdef _WIN32 TEST(StartupTest, WSAStartup) { @@ -345,6 +347,12 @@ protected: res.status = 404; } }) + .Post("/x-www-form-urlencoded-json", [&](const Request& req, Response& res) { + auto json = req.get_param_value("json"); + ASSERT_EQ(JSON_DATA, json); + res.set_content(json, "appliation/json"); + res.status = 200; + }) .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) { res.streamcb = [] (uint64_t offset) { if (offset < 3) @@ -572,6 +580,18 @@ TEST_F(ServerTest, PostMethod2) ASSERT_EQ("coder", res->body); } +TEST_F(ServerTest, PostWwwFormUrlEncodedJson) +{ + Params params; + params.emplace("json", JSON_DATA); + + auto res = cli_.Post("/x-www-form-urlencoded-json", params); + + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); + ASSERT_EQ(JSON_DATA, res->body); +} + TEST_F(ServerTest, PostEmptyContent) { auto res = cli_.Post("/empty", "", "text/plain");