multipart/form-dataに対応(#56)
This commit is contained in:
@@ -34,11 +34,20 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
// リクエストボディの構築
|
||||
std::string requestBody = "";
|
||||
{
|
||||
std::vector<std::string> tmp;
|
||||
for (const auto& [key, value] : bodyParam_) {
|
||||
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||
if (contentType_ == "application/x-www-form-urlencoded") {
|
||||
std::vector<std::string> tmp;
|
||||
for (const auto& [key, value] : bodyParam_) {
|
||||
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||
requestBody = CocoaTweet::Util::join(tmp, "&");
|
||||
}
|
||||
} else if (contentType_ == "multipart/form-data") {
|
||||
for (const auto& [key, value] : bodyParam_) {
|
||||
requestBody += (std::string("--") + "milkcocoa0902" + "\r\n");
|
||||
requestBody +=
|
||||
("Content-Disposition: form-data; name=\"" + key + "\";\r\n\r\n" + value + "\r\n");
|
||||
}
|
||||
requestBody += (std::string("--") + "milkcocoa0902" + "--" + "\r\n");
|
||||
}
|
||||
requestBody = CocoaTweet::Util::join(tmp, "&");
|
||||
}
|
||||
|
||||
// ヘッダの構築
|
||||
@@ -67,14 +76,22 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
|
||||
#ifndef NDEBUG
|
||||
std::cout << "requestBody : " << requestBody << std::endl;
|
||||
std::cout << "requestBody : " << requestBody << std::endl;
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
#endif
|
||||
// Headerを保持するcurl_slist*を初期化
|
||||
struct curl_slist* headers = NULL;
|
||||
// Authorizationをヘッダに追加
|
||||
headers = curl_slist_append(headers, oauthHeader.c_str());
|
||||
headers = curl_slist_append(headers, ("Content-Type: " + contentType_).c_str());
|
||||
|
||||
std::string contentType = "";
|
||||
if (contentType_ == "application/x-www-form-urlencoded") {
|
||||
contentType = contentType_;
|
||||
} else if (contentType_ == "multipart/form-data") {
|
||||
contentType = contentType_ + "; boundary=milkcocoa0902";
|
||||
}
|
||||
|
||||
headers = curl_slist_append(headers, ("Content-Type: " + contentType).c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
|
||||
|
||||
Reference in New Issue
Block a user