-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurly.cpp
48 lines (42 loc) · 1.33 KB
/
curly.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
#include "client.h"
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
if (argc > 1)
{
http_client client;
// assume first argument is the url
http_response response = client.get(argv[1]);
auto content_type = response.header("Content-Type");
if (content_type.find("text/") != string::npos ||
content_type == "application/json" ||
content_type == "application/xml")
{
// if 2 arguments, use the second as the filename to save it to
if (argc >= 3)
{
ofstream txtfile(argv[2]);
txtfile.write(response.body(), response.body_size());
}
else
{
// if no filename is supplied, print out the text
cout << response.body() << endl;
}
}
else
{
string filename = argc >= 3 ? argv[2] : "curly.bin";
if (content_type.find("image/png") != string::npos)
{
auto body = response.body();
auto size = response.body_size();
ofstream binfile(filename, ios::binary);
binfile.write(body, size);
binfile.close();
}
}
}
}