-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_file.cpp
More file actions
67 lines (59 loc) · 2.03 KB
/
Copy pathsend_file.cpp
File metadata and controls
67 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <pockethttp/pockethttp.hpp>
int main(int argc, char* argv[]) {
// Check arguments
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <url>" << std::endl;
return 1;
}
std::cout << "Uploading file README.md to " << argv[1] << std::endl;
// Create request
std::ifstream inputFile("README.md");
pockethttp::Request req;
req.method = "POST";
req.url = argv[1];
req.headers.set("Content-Type", "text/plain");
// No content length, chunked transfer encoding will be used
// req.headers.set("Content-Length", ...);
req.body_callback = [&inputFile](
unsigned char* data,
size_t* read_data,
const size_t max_size,
const size_t total_read) {
if (inputFile.eof()) {
*read_data = 0;
return false;
}
inputFile.read(reinterpret_cast<char*>(data), max_size);
*read_data = inputFile.gcount();
return true;
};
// Set response callback
pockethttp::Response res;
std::string resBody = "";
res.body_callback =
[&resBody](const unsigned char* buffer, const size_t& size) {
resBody.append(reinterpret_cast<const char*>(buffer), size);
};
// Create HTTP client
pockethttp::Http http;
int success = http.request(req, res);
if (success < 1) {
std::cerr << "Request failed: " << pockethttp::getErrorMessage(success)
<< std::endl;
std::cout << "Pulled body: " << std::endl;
std::cout << std::endl;
std::cout << resBody << std::endl;
std::cout << std::endl;
return 1;
}
std::cout << std::endl << std::endl;
std::cout << res.version << " " << res.status << " " << res.statusText
<< std::endl;
std::cout << res.headers.dump() << std::endl;
std::cout << resBody << std::endl << std::endl;
return 0;
}