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
+3 -2
View File
@@ -12,9 +12,10 @@ 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){
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;
+20 -3
View File
@@ -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 ";
@@ -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);
+1 -1
View File
@@ -6,7 +6,7 @@
#include <cocoatweet/api/interface/httpBase.h>
namespace CocoaTweet::API::Interface {
class HttpPost : public HttpBase{
class HttpPost : public HttpBase {
public:
protected:
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,