-
Notifications
You must be signed in to change notification settings - Fork 21
/
parser_test.cpp
60 lines (53 loc) · 1.91 KB
/
parser_test.cpp
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
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "multipart_parser.h"
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
int main(int argc, char* argv[])
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results")).then([=](ostream outFile)
{
*fileStream = outFile;
//Use MultipartParser to get the encoded body content and boundary
MultipartParser parser;
parser.AddParameter("Filename", "1.jpg");
parser.AddFile("file", "1.jpg");
std::string boundary = parser.boundary();
std::string body = parser.GenBodyContent();
std::cout << body << std::endl;
//Set up http client and request
http_request req;
http_client client(U("http://www.filedropper.com/index.php?xml=true"));
req.set_method(web::http::methods::POST);
req.set_body(body, "multipart/form-data; boundary=" + boundary);
return client.request(req);
})
.then([=](pplx::task<http_response> response_task)
{
http_response response = response_task.get();
return response.body().read_to_end(fileStream->streambuf());
})
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
return 0;
}