Refine form data
This commit is contained in:
@@ -50,8 +50,14 @@ int main(int argc, char* argv[]) {
|
|||||||
webcc::HttpClientSession session;
|
webcc::HttpClientSession session;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto r = session.PostFile(url, "file",
|
// auto r = session.PostFile(url, "file",
|
||||||
upload_dir / "remember.txt");
|
// 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;
|
//std::cout << r->content() << std::endl;
|
||||||
|
|
||||||
|
|||||||
+3
-16
@@ -215,22 +215,9 @@ FormPart::FormPart(const std::string& name, const Path& path,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FormPart::FormPart(std::string&& data, const std::string& file_name,
|
FormPart::FormPart(const std::string& name, std::string&& data,
|
||||||
const std::string& mime_type) {
|
const std::string& mime_type)
|
||||||
data_ = std::move(data);
|
: name_(name), data_(std::move(data)), mime_type_(mime_type) {
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormPart::Prepare(std::vector<boost::asio::const_buffer>& payload) {
|
void FormPart::Prepare(std::vector<boost::asio::const_buffer>& payload) {
|
||||||
|
|||||||
+1
-1
@@ -159,7 +159,7 @@ public:
|
|||||||
explicit FormPart(const std::string& name, const Path& path,
|
explicit FormPart(const std::string& name, const Path& path,
|
||||||
const std::string& mime_type = "");
|
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 = "");
|
const std::string& mime_type = "");
|
||||||
|
|
||||||
#if WEBCC_DEFAULT_MOVE_COPY_ASSIGN
|
#if WEBCC_DEFAULT_MOVE_COPY_ASSIGN
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public:
|
|||||||
return form_parts_;
|
return form_parts_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_form_parts_(std::vector<FormPart>&& form_parts) {
|
void set_form_parts(std::vector<FormPart>&& form_parts) {
|
||||||
form_parts_ = std::move(form_parts);
|
form_parts_ = std::move(form_parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,20 +33,8 @@ HttpRequestPtr HttpRequestBuilder::Build() {
|
|||||||
if (json_) {
|
if (json_) {
|
||||||
request->SetContentType(http::media_types::kApplicationJson, "");
|
request->SetContentType(http::media_types::kApplicationJson, "");
|
||||||
}
|
}
|
||||||
} else if (!files_.empty()) {
|
} else if (!form_parts_.empty()) {
|
||||||
request->set_form_parts_(std::move(files_));
|
request->set_form_parts(std::move(form_parts_));
|
||||||
|
|
||||||
//// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
@@ -56,21 +44,15 @@ HttpRequestBuilder& HttpRequestBuilder::File(const std::string& name,
|
|||||||
const Path& path,
|
const Path& path,
|
||||||
const std::string& mime_type) {
|
const std::string& mime_type) {
|
||||||
assert(!name.empty());
|
assert(!name.empty());
|
||||||
|
form_parts_.push_back(FormPart{ name, path, mime_type });
|
||||||
files_.push_back(FormPart{ name, path, mime_type });
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpRequestBuilder& HttpRequestBuilder::FileData(const std::string& name,
|
HttpRequestBuilder& HttpRequestBuilder::Form(const std::string& name,
|
||||||
std::string&& file_data,
|
std::string&& data,
|
||||||
const std::string& file_name,
|
const std::string& mime_type) {
|
||||||
const std::string& mime_type) {
|
|
||||||
assert(!name.empty());
|
assert(!name.empty());
|
||||||
|
form_parts_.push_back(FormPart{ name, std::move(data), mime_type });
|
||||||
// TODO
|
|
||||||
//files_[name] = HttpFile(std::move(file_data), file_name, mime_type);
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,21 +70,17 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload a file with its path.
|
// Upload a file.
|
||||||
HttpRequestBuilder& File(const std::string& name, const Path& path,
|
HttpRequestBuilder& File(const std::string& name, const Path& path,
|
||||||
const std::string& mime_type = "");
|
const std::string& mime_type = "");
|
||||||
|
|
||||||
HttpRequestBuilder& File(FormPart&& file) {
|
HttpRequestBuilder& Form(FormPart&& part) {
|
||||||
files_.push_back(std::move(file));
|
form_parts_.push_back(std::move(part));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload a file with its data.
|
HttpRequestBuilder& Form(const std::string& name, std::string&& data,
|
||||||
// TODO: Unicode |file_name|.
|
const std::string& mime_type = "");
|
||||||
HttpRequestBuilder& FileData(const std::string& name,
|
|
||||||
std::string&& file_data,
|
|
||||||
const std::string& file_name = "",
|
|
||||||
const std::string& mime_type = "");
|
|
||||||
|
|
||||||
HttpRequestBuilder& Gzip(bool gzip = true) {
|
HttpRequestBuilder& Gzip(bool gzip = true) {
|
||||||
gzip_ = gzip;
|
gzip_ = gzip;
|
||||||
@@ -129,7 +125,7 @@ private:
|
|||||||
bool json_ = false;
|
bool json_ = false;
|
||||||
|
|
||||||
// Files to upload for a POST request.
|
// Files to upload for a POST request.
|
||||||
std::vector<FormPart> files_;
|
std::vector<FormPart> form_parts_;
|
||||||
|
|
||||||
// Compress the request content.
|
// Compress the request content.
|
||||||
// NOTE: Most servers don't support compressed requests.
|
// NOTE: Most servers don't support compressed requests.
|
||||||
|
|||||||
Reference in New Issue
Block a user