You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
![]()
6 years ago
|
// A client posting multipart form data.
|
||
|
|
||
![]()
6 years ago
|
#include <iostream>
|
||
|
|
||
|
#include "boost/filesystem.hpp"
|
||
|
|
||
![]()
6 years ago
|
#include "webcc/client_session.h"
|
||
![]()
6 years ago
|
#include "webcc/logger.h"
|
||
|
|
||
|
int main(int argc, char* argv[]) {
|
||
|
if (argc < 2) {
|
||
![]()
6 years ago
|
std::cout << "usage: form_client <upload_dir> [url]" << std::endl;
|
||
![]()
6 years ago
|
std::cout << std::endl;
|
||
|
std::cout << "default url: http://httpbin.org/post" << std::endl;
|
||
|
std::cout << std::endl;
|
||
|
std::cout << "examples:" << std::endl;
|
||
![]()
6 years ago
|
std::cout << " $ form_client E:/github/webcc/data/upload" << std::endl;
|
||
|
std::cout << " $ form_client E:/github/webcc/data/upload "
|
||
![]()
6 years ago
|
<< "http://httpbin.org/post" << std::endl;
|
||
![]()
6 years ago
|
return 1;
|
||
|
}
|
||
|
|
||
|
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||
|
|
||
|
const webcc::Path upload_dir(argv[1]);
|
||
|
|
||
![]()
6 years ago
|
std::string url;
|
||
|
if (argc == 3) {
|
||
|
url = argv[2];
|
||
|
} else {
|
||
|
url = "http://httpbin.org/post";
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
namespace bfs = boost::filesystem;
|
||
|
|
||
|
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
|
||
|
std::cerr << "Invalid upload dir!" << std::endl;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
![]()
6 years ago
|
webcc::ClientSession session;
|
||
![]()
6 years ago
|
|
||
|
try {
|
||
![]()
6 years ago
|
auto r = session.Request(webcc::RequestBuilder{}.
|
||
|
Post(url).
|
||
![]()
6 years ago
|
FormFile("file", upload_dir / "remember.txt").
|
||
|
FormData("json", "{}", "application/json")
|
||
![]()
6 years ago
|
());
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
std::cout << r->status() << std::endl;
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
} catch (const webcc::Error& error) {
|
||
|
std::cout << error << std::endl;
|
||
![]()
6 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|