allow making POST requests with empty body

This commit is contained in:
jacob1 2020-01-20 11:11:56 -05:00
parent afefd045c8
commit 0c588c48fe
3 changed files with 13 additions and 8 deletions

View File

@ -56,6 +56,8 @@ namespace http
// add post data to a request
void Request::AddPostData(std::map<ByteString, ByteString> data)
{
// Even if the map is empty, calling this function signifies you want to do a POST request
isPost = true;
#ifndef NOHTTP
if (!data.size())
{
@ -134,10 +136,6 @@ namespace http
{
curl_easy_setopt(easy, CURLOPT_MIMEPOST, post_fields);
}
else
{
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
}
#else
if (!post_fields_map.empty())
{
@ -163,11 +161,16 @@ namespace http
}
curl_easy_setopt(easy, CURLOPT_HTTPPOST, post_fields_first);
}
#endif
else if (isPost)
{
curl_easy_setopt(easy, CURLOPT_POST, 1);
curl_easy_setopt(easy, CURLOPT_POSTFIELDS, "");
}
else
{
curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
}
#endif
curl_easy_setopt(easy, CURLOPT_FOLLOWLOCATION, 1L);
#ifdef ENFORCE_HTTPS

View File

@ -47,6 +47,7 @@ namespace http
struct curl_slist *headers;
bool isPost = false;
#ifdef REQUEST_USE_CURL_MIMEPOST
curl_mime *post_fields;
#else

View File

@ -3694,7 +3694,7 @@ class RequestHandle
bool dead;
public:
RequestHandle(ByteString &uri, std::map<ByteString, ByteString> &post_data, std::map<ByteString, ByteString> &headers)
RequestHandle(ByteString &uri, bool isPost, std::map<ByteString, ByteString> &post_data, std::map<ByteString, ByteString> &headers)
{
dead = false;
request = new http::Request(uri);
@ -3702,7 +3702,8 @@ public:
{
request->AddHeader(header.first, header.second);
}
request->AddPostData(post_data);
if (isPost)
request->AddPostData(post_data);
request->Start();
}
@ -3853,7 +3854,7 @@ static int http_request(lua_State *l, bool isPost)
{
return 0;
}
new(rh) RequestHandle(uri, post_data, headers);
new(rh) RequestHandle(uri, isPost, post_data, headers);
luaL_newmetatable(l, "HTTPRequest");
lua_setmetatable(l, -2);
return 1;