Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions minihttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down Expand Up @@ -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())
Expand All @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion minihttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down