From 5b2cc76afdd06643680062e0e2fabc37688ec652 Mon Sep 17 00:00:00 2001 From: meh2481 Date: Sat, 27 Aug 2016 09:54:22 -0400 Subject: [PATCH 1/2] Add support for POSTing JSON --- example2.cpp | 8 ++++---- minihttp.cpp | 18 +++++++++++++++--- minihttp.h | 5 ++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/example2.cpp b/example2.cpp index 2e97688..bd68086 100644 --- a/example2.cpp +++ b/example2.cpp @@ -72,10 +72,10 @@ int main(int argc, char *argv[]) // Example HTTP POST request: minihttp::POST post; - post.add("a", "b"); - post.add("x", "y"); - post.add("long string", "possibly invalid data: /x/&$+*#'?!;"); - post.add("normal", "data"); + post.addFormData("a", "b"); + post.addFormData("x", "y"); + post.addFormData("long string", "possibly invalid data: /x/&$+*#'?!;"); + post.addFormData("normal", "data"); ht->Download("https://httpbin.org/post", NULL, NULL, &post); minihttp::SocketSet ss; diff --git a/minihttp.cpp b/minihttp.cpp index 12d247c..f92246c 100644 --- a/minihttp.cpp +++ b/minihttp.cpp @@ -788,8 +788,10 @@ static void strToLower(std::string& s) std::transform(s.begin(), s.end(), s.begin(), tolower); } -POST& POST::add(const char *key, const char *value) +POST& POST::addFormData(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..5f7dcb0 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& addFormData(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 From 8343e9d5df55500a8a5ec9d8b955173f53bdca8f Mon Sep 17 00:00:00 2001 From: meh2481 Date: Sat, 27 Aug 2016 09:59:09 -0400 Subject: [PATCH 2/2] Should prolly leave API the same... --- example2.cpp | 8 ++++---- minihttp.cpp | 2 +- minihttp.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example2.cpp b/example2.cpp index bd68086..2e97688 100644 --- a/example2.cpp +++ b/example2.cpp @@ -72,10 +72,10 @@ int main(int argc, char *argv[]) // Example HTTP POST request: minihttp::POST post; - post.addFormData("a", "b"); - post.addFormData("x", "y"); - post.addFormData("long string", "possibly invalid data: /x/&$+*#'?!;"); - post.addFormData("normal", "data"); + post.add("a", "b"); + post.add("x", "y"); + post.add("long string", "possibly invalid data: /x/&$+*#'?!;"); + post.add("normal", "data"); ht->Download("https://httpbin.org/post", NULL, NULL, &post); minihttp::SocketSet ss; diff --git a/minihttp.cpp b/minihttp.cpp index f92246c..94d1a2b 100644 --- a/minihttp.cpp +++ b/minihttp.cpp @@ -788,7 +788,7 @@ static void strToLower(std::string& s) std::transform(s.begin(), s.end(), s.begin(), tolower); } -POST& POST::addFormData(const char *key, const char *value) +POST& POST::add(const char *key, const char *value) { if(json) data.clear(); diff --git a/minihttp.h b/minihttp.h index 5f7dcb0..028ec2f 100644 --- a/minihttp.h +++ b/minihttp.h @@ -141,7 +141,7 @@ class POST { public: void reserve(size_t res) { data.reserve(res); } - POST& addFormData(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; }