diff --git a/minihttp.cpp b/minihttp.cpp index 12d247c..94d1a2b 100644 --- a/minihttp.cpp +++ b/minihttp.cpp @@ -790,6 +790,8 @@ static void strToLower(std::string& s) POST& POST::add(const char *key, const char *value) { + if(json) + data.clear(); if(!empty()) data += '&'; URLEncode(key, data); @@ -798,6 +800,13 @@ POST& POST::add(const char *key, const char *value) return *this; } +POST & POST::setJsonData(const char * value) +{ + data = value; + json = true; + return *this; +} + HttpSocket::HttpSocket() : TcpSocket(), @@ -926,7 +935,10 @@ bool HttpSocket::SendRequest(Request& req, bool enqueue) if(post) { r << "Content-Length: " << req.post.length() << crlf; - r << "Content-Type: application/x-www-form-urlencoded" << crlf; + if(req.post.isJson()) + r << "Content-Type: application/json" << crlf; + else + r << "Content-Type: application/x-www-form-urlencoded" << crlf; } if(req.extraGetHeaders.length()) @@ -938,7 +950,7 @@ bool HttpSocket::SendRequest(Request& req, bool enqueue) r << crlf; // header terminator - // FIXME: appending this to the 'header' field is probably not a good idea + //Add data to POST if(post) r << req.post.str(); diff --git a/minihttp.h b/minihttp.h index 7bfa035..028ec2f 100644 --- a/minihttp.h +++ b/minihttp.h @@ -141,13 +141,16 @@ class POST { public: void reserve(size_t res) { data.reserve(res); } - POST& add(const char *key, const char *value); + POST& add(const char *key, const char *value); + POST& setJsonData(const char *value); const char *c_str() const { return data.c_str(); } const std::string& str() const { return data; } bool empty() const { return data.empty(); } size_t length() const { return data.length(); } + bool isJson() const { return json; } private: std::string data; + bool json = false; }; struct Request