improve CommonClient.buildRpcHttpRequest
Supported set body of request.
This commit is contained in:
committed by
Jackson Tian
parent
9b0cec3ab7
commit
9e1e045351
@@ -291,13 +291,22 @@ HttpRequest CommonClient::buildRpcHttpRequest(const std::string &endpoint,
|
||||
queryParams["Timestamp"] = ss.str();
|
||||
queryParams["Version"] = msg.version();
|
||||
|
||||
std::string bodyParamString;
|
||||
auto signParams = queryParams;
|
||||
auto bodyParams = msg.bodyParameters();
|
||||
for (const auto &p : bodyParams)
|
||||
{
|
||||
bodyParamString += "&";
|
||||
bodyParamString += (p.first + "=" + UrlEncode(p.second));
|
||||
signParams[p.first] = p.second;
|
||||
}
|
||||
|
||||
std::stringstream plaintext;
|
||||
plaintext << HttpMethodToString(method)
|
||||
<< "&"
|
||||
<< UrlEncode(url.path())
|
||||
<< "&"
|
||||
<< UrlEncode(canonicalizedQuery(queryParams));
|
||||
|
||||
<< UrlEncode(canonicalizedQuery(signParams));
|
||||
queryParams["Signature"] = signer_->generate(plaintext.str(),
|
||||
credentials.accessKeySecret() + "&");
|
||||
|
||||
@@ -329,6 +338,11 @@ HttpRequest CommonClient::buildRpcHttpRequest(const std::string &endpoint,
|
||||
request.setHeader("Host", url.host());
|
||||
request.setHeader("x-sdk-client",
|
||||
std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
|
||||
|
||||
if (!bodyParamString.empty())
|
||||
{
|
||||
request.setBody(bodyParamString.c_str() + 1, bodyParamString.size() - 1);
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
@@ -159,6 +159,22 @@ namespace {
|
||||
ShutdownSdk();
|
||||
}
|
||||
|
||||
TEST(CommonClient, bodyParam) {
|
||||
const ClientConfiguration cfg;
|
||||
|
||||
std::string key = "accessKeyId";
|
||||
std::string secret = "accessSecret";
|
||||
std::string token = "sessionToken";
|
||||
|
||||
const Credentials credentials(key, secret, token);
|
||||
CommonRequest cr;
|
||||
cr.setBodyParameter("TestBodyParamKey", "TestBodyParamValue");
|
||||
TestCommonClient* client = new TestCommonClient(credentials, cfg);
|
||||
HttpRequest r = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
|
||||
const char *s = "TestBodyParamKey=TestBodyParamValue";
|
||||
EXPECT_EQ(*r.body(), *s);
|
||||
}
|
||||
|
||||
TEST(CommonClient, abnormal) {
|
||||
InitializeSdk();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user