multipart/form-dataに対応(#56)
This commit is contained in:
@@ -12,7 +12,8 @@ protected:
|
||||
std::map<std::string, std::string> bodyParam_;
|
||||
std::string url_;
|
||||
std::string contentType_;
|
||||
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
virtual void process(
|
||||
std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
std::function<void(const unsigned int, const std::string&)> _callback) = 0;
|
||||
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) {
|
||||
int realsize = _size * _nmemb;
|
||||
|
||||
@@ -34,12 +34,21 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
// リクエストボディの構築
|
||||
std::string requestBody = "";
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
// ヘッダの構築
|
||||
std::string oauthHeader = "authorization: OAuth ";
|
||||
@@ -74,7 +83,15 @@ std::cout << "requestBody : " << requestBody << std::endl;
|
||||
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