multipart/form-dataに対応(#56)

This commit is contained in:
keita
2021-03-10 23:57:52 +09:00
parent 3c99ccab3e
commit a58903e9c0
3 changed files with 29 additions and 11 deletions
+5 -4
View File
@@ -12,13 +12,14 @@ protected:
std::map<std::string, std::string> bodyParam_; std::map<std::string, std::string> bodyParam_;
std::string url_; std::string url_;
std::string contentType_; std::string contentType_;
virtual void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, virtual void process(
std::function<void(const unsigned int, const std::string&)> _callback) = 0; std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream){ 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; int realsize = _size * _nmemb;
_stream->append(_ptr, realsize); _stream->append(_ptr, realsize);
return realsize; return realsize;
} }
}; };
} // namespace CocoaTweet::API::Interface } // namespace CocoaTweet::API::Interface
+23 -6
View File
@@ -34,11 +34,20 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
// リクエストボディの構築 // リクエストボディの構築
std::string requestBody = ""; std::string requestBody = "";
{ {
std::vector<std::string> tmp; if (contentType_ == "application/x-www-form-urlencoded") {
for (const auto& [key, value] : bodyParam_) { std::vector<std::string> tmp;
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value)); 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_WRITEFUNCTION, curlCallback_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "requestBody : " << requestBody << std::endl; std::cout << "requestBody : " << requestBody << std::endl;
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif #endif
// Headerを保持するcurl_slist*を初期化 // Headerを保持するcurl_slist*を初期化
struct curl_slist* headers = NULL; struct curl_slist* headers = NULL;
// Authorizationをヘッダに追加 // Authorizationをヘッダに追加
headers = curl_slist_append(headers, oauthHeader.c_str()); 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); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
+1 -1
View File
@@ -6,7 +6,7 @@
#include <cocoatweet/api/interface/httpBase.h> #include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface { namespace CocoaTweet::API::Interface {
class HttpPost : public HttpBase{ class HttpPost : public HttpBase {
public: public:
protected: protected:
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth, void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,