Fix null jsonObject from newHttpJsonRequest (#388)

* Fix null jsonObject from newHttpJsonRequest 

Refer to #372 for the response case as well
This commit is contained in:
ihmc3jn09hk 2020-03-15 00:11:46 +08:00 committed by GitHub
parent 9ee00da431
commit 2401c6a88a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -193,6 +193,7 @@ void doTest(const HttpClientPtr &client,
}); });
// Post json again // Post json again
req = HttpRequest::newHttpJsonRequest(json); req = HttpRequest::newHttpJsonRequest(json);
assert(req->jsonObject());
req->setMethod(drogon::Post); req->setMethod(drogon::Post);
req->setPath("/api/v1/apitest/json"); req->setPath("/api/v1/apitest/json");
client->sendRequest(req, client->sendRequest(req,

View File

@ -29,9 +29,9 @@ void HttpRequestImpl::parseJson() const
auto input = contentView(); auto input = contentView();
if (input.empty()) if (input.empty())
return; return;
std::string type = getHeaderBy("content-type"); if (contentType_ == CT_APPLICATION_JSON ||
std::transform(type.begin(), type.end(), type.begin(), tolower); getHeaderBy("content-type").find("application/json") !=
if (type.find("application/json") != std::string::npos) std::string::npos)
{ {
static std::once_flag once; static std::once_flag once;
static Json::CharReaderBuilder builder; static Json::CharReaderBuilder builder;
@ -48,6 +48,10 @@ void HttpRequestImpl::parseJson() const
jsonPtr_.reset(); jsonPtr_.reset();
} }
} }
else
{
jsonPtr_.reset();
}
} }
void HttpRequestImpl::parseParameters() const void HttpRequestImpl::parseParameters() const
{ {
@ -628,4 +632,4 @@ void HttpRequestImpl::reserveBodySize()
.append(fileName); .append(fileName);
cacheFilePtr_ = std::make_unique<CacheFile>(tmpfile); cacheFilePtr_ = std::make_unique<CacheFile>(tmpfile);
} }
} }