Refine form data
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
+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,
|
||||
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<boost::asio::const_buffer>& payload) {
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<FormPart> files_;
|
||||
std::vector<FormPart> form_parts_;
|
||||
|
||||
// Compress the request content.
|
||||
// NOTE: Most servers don't support compressed requests.
|
||||
|
||||
Reference in New Issue
Block a user