diff --git a/examples/file_upload_client.cc b/examples/file_upload_client.cc index 495f6ac..2c6749a 100644 --- a/examples/file_upload_client.cc +++ b/examples/file_upload_client.cc @@ -50,8 +50,14 @@ int main(int argc, char* argv[]) { webcc::HttpClientSession session; try { - auto r = session.PostFile(url, "file", - upload_dir / "remember.txt"); + // auto r = session.PostFile(url, "file", + // upload_dir / "remember.txt"); + + auto r = session.Request(webcc::HttpRequestBuilder{}.Post(). + Url(url). + File("file", upload_dir / "remember.txt"). + Form("text", "text default") + ()); //std::cout << r->content() << std::endl; diff --git a/webcc/common.cc b/webcc/common.cc index 39d7871..b1eecad 100644 --- a/webcc/common.cc +++ b/webcc/common.cc @@ -215,22 +215,9 @@ FormPart::FormPart(const std::string& name, const Path& path, } } -FormPart::FormPart(std::string&& data, const std::string& file_name, - const std::string& mime_type) { - data_ = std::move(data); - - file_name_ = file_name; - - mime_type_ = mime_type; - - // Determine content type from file extension. - if (mime_type_.empty()) { - std::size_t pos = file_name_.find_last_of('.'); - if (pos != std::string::npos) { - std::string extension = file_name_.substr(pos + 1); - mime_type_ = http::media_types::FromExtension(extension, false); - } - } +FormPart::FormPart(const std::string& name, std::string&& data, + const std::string& mime_type) + : name_(name), data_(std::move(data)), mime_type_(mime_type) { } void FormPart::Prepare(std::vector& payload) { diff --git a/webcc/common.h b/webcc/common.h index d458b32..3c236b1 100644 --- a/webcc/common.h +++ b/webcc/common.h @@ -159,7 +159,7 @@ public: explicit FormPart(const std::string& name, const Path& path, const std::string& mime_type = ""); - FormPart(std::string&& data, const std::string& file_name, + FormPart(const std::string& name, std::string&& data, const std::string& mime_type = ""); #if WEBCC_DEFAULT_MOVE_COPY_ASSIGN diff --git a/webcc/http_request.h b/webcc/http_request.h index 8b6eecc..849afa3 100644 --- a/webcc/http_request.h +++ b/webcc/http_request.h @@ -59,7 +59,7 @@ public: return form_parts_; } - void set_form_parts_(std::vector&& form_parts) { + void set_form_parts(std::vector&& form_parts) { form_parts_ = std::move(form_parts); } diff --git a/webcc/http_request_builder.cc b/webcc/http_request_builder.cc index 2cd0c60..31c85bf 100644 --- a/webcc/http_request_builder.cc +++ b/webcc/http_request_builder.cc @@ -33,20 +33,8 @@ HttpRequestPtr HttpRequestBuilder::Build() { if (json_) { request->SetContentType(http::media_types::kApplicationJson, ""); } - } else if (!files_.empty()) { - request->set_form_parts_(std::move(files_)); - - //// Another choice to generate the boundary is what Apache does. - //// See: https://stackoverflow.com/a/5686863 - //const std::string boundary = RandomUuid(); - - //request->SetContentType("multipart/form-data; boundary=" + boundary); - - //std::string data; - //CreateFormData(&data, boundary); - - //// Ingore gzip since most servers don't support it. - //request->SetContent(std::move(data), true); + } else if (!form_parts_.empty()) { + request->set_form_parts(std::move(form_parts_)); } return request; @@ -56,21 +44,15 @@ HttpRequestBuilder& HttpRequestBuilder::File(const std::string& name, const Path& path, const std::string& mime_type) { assert(!name.empty()); - - files_.push_back(FormPart{ name, path, mime_type }); - + form_parts_.push_back(FormPart{ name, path, mime_type }); return *this; } -HttpRequestBuilder& HttpRequestBuilder::FileData(const std::string& name, - std::string&& file_data, - const std::string& file_name, - const std::string& mime_type) { +HttpRequestBuilder& HttpRequestBuilder::Form(const std::string& name, + std::string&& data, + const std::string& mime_type) { assert(!name.empty()); - - // TODO - //files_[name] = HttpFile(std::move(file_data), file_name, mime_type); - + form_parts_.push_back(FormPart{ name, std::move(data), mime_type }); return *this; } diff --git a/webcc/http_request_builder.h b/webcc/http_request_builder.h index ff80bdb..13f6f22 100644 --- a/webcc/http_request_builder.h +++ b/webcc/http_request_builder.h @@ -70,21 +70,17 @@ public: return *this; } - // Upload a file with its path. + // Upload a file. HttpRequestBuilder& File(const std::string& name, const Path& path, const std::string& mime_type = ""); - HttpRequestBuilder& File(FormPart&& file) { - files_.push_back(std::move(file)); + HttpRequestBuilder& Form(FormPart&& part) { + form_parts_.push_back(std::move(part)); return *this; } - // Upload a file with its data. - // TODO: Unicode |file_name|. - HttpRequestBuilder& FileData(const std::string& name, - std::string&& file_data, - const std::string& file_name = "", - const std::string& mime_type = ""); + HttpRequestBuilder& Form(const std::string& name, std::string&& data, + const std::string& mime_type = ""); HttpRequestBuilder& Gzip(bool gzip = true) { gzip_ = gzip; @@ -129,7 +125,7 @@ private: bool json_ = false; // Files to upload for a POST request. - std::vector files_; + std::vector form_parts_; // Compress the request content. // NOTE: Most servers don't support compressed requests.