diff --git a/src/cocoatweet/api/interface/httpBase.h b/src/cocoatweet/api/interface/httpBase.h index b52ce83..b5dffc7 100644 --- a/src/cocoatweet/api/interface/httpBase.h +++ b/src/cocoatweet/api/interface/httpBase.h @@ -12,13 +12,14 @@ protected: std::map bodyParam_; std::string url_; std::string contentType_; - virtual void process(std::weak_ptr _oauth, - std::function _callback) = 0; - static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream){ + virtual void process( + std::weak_ptr _oauth, + std::function _callback) = 0; + static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) { int realsize = _size * _nmemb; _stream->append(_ptr, realsize); return realsize; - } + } }; } // namespace CocoaTweet::API::Interface diff --git a/src/cocoatweet/api/interface/httpPost.cc b/src/cocoatweet/api/interface/httpPost.cc index 7a1d208..187b2cb 100644 --- a/src/cocoatweet/api/interface/httpPost.cc +++ b/src/cocoatweet/api/interface/httpPost.cc @@ -34,11 +34,20 @@ void HttpPost::process(std::weak_ptr _oauth, // リクエストボディの構築 std::string requestBody = ""; { - std::vector tmp; - for (const auto& [key, value] : bodyParam_) { - tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); + if (contentType_ == "application/x-www-form-urlencoded") { + std::vector 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 _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); diff --git a/src/cocoatweet/api/interface/httpPost.h b/src/cocoatweet/api/interface/httpPost.h index d38fcd3..e4768e7 100644 --- a/src/cocoatweet/api/interface/httpPost.h +++ b/src/cocoatweet/api/interface/httpPost.h @@ -6,7 +6,7 @@ #include namespace CocoaTweet::API::Interface { -class HttpPost : public HttpBase{ +class HttpPost : public HttpBase { public: protected: void process(std::weak_ptr _oauth,