improve the parsing of form data
This commit is contained in:
@@ -8,13 +8,14 @@
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
std::cout << "usage: file_downloader <url> <path>" << std::endl;
|
||||
std::cout << "Usage: file_downloader <url> <path>" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "examples:" << std::endl;
|
||||
std::cout << " $ file_downloader http://httpbin.org/image/jpeg D:/test.jpg"
|
||||
<< std::endl;
|
||||
std::cout << " $ file_downloader https://www.google.com/favicon.ico"
|
||||
<< " D:/test.ico" << std::endl;
|
||||
std::cout << "Examples:" << std::endl;
|
||||
std::cout
|
||||
<< " $ ./file_downloader http://httpbin.org/image/jpeg ~/test.jpg"
|
||||
<< std::endl;
|
||||
std::cout << " $ ./file_downloader https://www.google.com/favicon.ico"
|
||||
<< " ~/test.ico" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
+14
-9
@@ -11,14 +11,19 @@ namespace bfs = boost::filesystem;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cout << "usage: form_client <upload_dir> [url]" << std::endl;
|
||||
std::cout << "Usage: form_client <upload_dir> [url]" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "default url: http://httpbin.org/post" << std::endl;
|
||||
std::cout << "Default url: http://httpbin.org/post" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "examples:" << std::endl;
|
||||
std::cout << " $ form_client E:/github/webcc/data/upload" << std::endl;
|
||||
std::cout << " $ form_client E:/github/webcc/data/upload "
|
||||
std::cout << "Examples:" << std::endl;
|
||||
std::cout << "(Post to httpbin.org)" << std::endl;
|
||||
std::cout << " $ ./form_client path/to/webcc/data/upload" << std::endl;
|
||||
std::cout << " $ ./form_client path/to/webcc/data/upload "
|
||||
<< "http://httpbin.org/post" << std::endl;
|
||||
std::cout << "(Post the example 'form_server')" << std::endl;
|
||||
std::cout << " $ ./form_client path/to/webcc/data/upload "
|
||||
"http://localhost:8080/upload"
|
||||
<< std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -41,10 +46,10 @@ int main(int argc, char* argv[]) {
|
||||
webcc::ClientSession session;
|
||||
|
||||
try {
|
||||
auto r = session.Send(webcc::RequestBuilder{}.Post(url).
|
||||
FormFile("file", upload_dir / "remember.txt").
|
||||
FormData("json", "{}", "application/json")
|
||||
());
|
||||
auto r = session.Send(webcc::RequestBuilder{}
|
||||
.Post(url)
|
||||
.FormFile("file", upload_dir / "remember.txt")
|
||||
.FormData("json", "{}", "application/json")());
|
||||
|
||||
std::cout << r->status() << std::endl;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// A server handling multipart form data.
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
@@ -25,7 +26,13 @@ private:
|
||||
|
||||
for (auto& part : request->form_parts()) {
|
||||
std::cout << "name: " << part->name() << std::endl;
|
||||
std::cout << "data: " << std::endl << part->data() << std::endl;
|
||||
|
||||
if (part->file_name().empty()) {
|
||||
std::cout << "data: " << part->data() << std::endl;
|
||||
} else {
|
||||
// Save part->data() as binary to file.
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
return webcc::ResponseBuilder{}.Created().Body("OK")();
|
||||
@@ -36,7 +43,7 @@ private:
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cout << "usage: file_upload_server <port>" << std::endl;
|
||||
std::cout << "usage: form_server <port>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user