yhirose 7 years ago
parent
commit
86b3dfc480
2 changed files with 21 additions and 6 deletions
  1. 1 6
      httplib.h
  2. 20 0
      test/test.cc

+ 1 - 6
httplib.h

@@ -2068,12 +2068,7 @@ inline void Client::write_request(Stream& strm, Request& req)
 
 
     // Body
     // Body
     if (!req.body.empty()) {
     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
     // Flush buffer

+ 20 - 0
test/test.cc

@@ -23,6 +23,8 @@ const int   PORT = 1234;
 const string LONG_QUERY_VALUE = string(25000, '@');
 const string LONG_QUERY_VALUE = string(25000, '@');
 const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
 const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
 
 
+const std::string JSON_DATA = "{\"hello\":\"world\"}";
+
 #ifdef _WIN32
 #ifdef _WIN32
 TEST(StartupTest, WSAStartup)
 TEST(StartupTest, WSAStartup)
 {
 {
@@ -345,6 +347,12 @@ protected:
                     res.status = 404;
                     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) {
             .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) {
                 res.streamcb = [] (uint64_t offset) {
                 res.streamcb = [] (uint64_t offset) {
                     if (offset < 3)
                     if (offset < 3)
@@ -572,6 +580,18 @@ TEST_F(ServerTest, PostMethod2)
     ASSERT_EQ("coder", res->body);
     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)
 TEST_F(ServerTest, PostEmptyContent)
 {
 {
     auto res = cli_.Post("/empty", "", "text/plain");
     auto res = cli_.Post("/empty", "", "text/plain");