-
Notifications
You must be signed in to change notification settings - Fork 17
/
examples.js
214 lines (197 loc) · 4.28 KB
/
examples.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var httpreq = require('./lib/httpreq');
fs = require('fs')
// example1(); // get www.google.com
// example2(); // do some post
// example3(); // same as above + extra headers + cookies
// example4(); // https also works:
// example5(); // uploading some file:
// example6(); // u can use doRequest instead of .get or .post
// example7(); // download a binary file:
// example8(); // send json
// example9(); // send your own body content (eg. xml)
// example10(); // set max redirects:
// example11(); // set timeout
// example12(); // // download file directly to disk
// get www.google.com
function example1(){
httpreq.get('http://www.google.com', function (err, res){
if (err){
console.log(err);
}else{
console.log(res.headers); //headers are stored in res.headers
console.log(res.body); //content of the body is stored in res.body
}
});
}
// do some post
function example2(){
httpreq.post('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
}
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
}
// same as above + extra headers + cookies
function example3(){
httpreq.post('http://posttestserver.com/post.php', {
parameters: {
name: 'John',
lastname: 'Doe'
},
headers:{
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
},
cookies: [
'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
'id=2'
]
}, function (err, res){
if (err){
console.log(err);
}else{
console.log(res.body);
}
});
}
// https also works:
function example4(){
httpreq.get('https://graph.facebook.com/19292868552', function (err, res){
if (err){
console.log(err);
}else{
console.log(JSON.parse(res.body));
}
});
}
// uploading some file:
function example5(){
httpreq.uploadFiles({
url: "http://rekognition.com/demo/do_upload/",
parameters:{
name_space : 'something',
},
files:{
fileToUpload: __dirname + "/test/testupload.jpg"
}},
function (err, res){
if (err) return console.log(err);
console.log(res.body);
});
}
// u can use doRequest instead of .get or .post
function example6(){
httpreq.doRequest({
url: 'https://graph.facebook.com/19292868552',
method: 'GET',
parameters: {
name: 'test'
}
},
function (err, res){
if (err){
console.log(err);
}else{
console.log(JSON.parse(res.body));
}
});
}
// download a binary file:
function example7(){
httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {
binary: true,
progressCallback: function (err, progress) {
console.log(progress);
}
},
function (err, res){
if (err){
console.log(err);
}else{
fs.writeFile(__dirname + '/test.png', res.body, function (err) {
if(err) return console.log("error writing file");
});
}
});
}
// send json
function example8(){
httpreq.post('http://posttestserver.com/post.php',{
json: {name: 'John', lastname: 'Do'},
headers:{
'Content-Type': 'text/xml',
}},
function (err, res) {
if (err){
console.log(err);
}else{
console.log(res.body);
}
}
);
}
// send your own body content (eg. xml):
function example9(){
httpreq.post('http://posttestserver.com/post.php',{
body: '<?xml version="1.0" encoding="UTF-8"?>',
headers:{
'Content-Type': 'text/xml',
}},
function (err, res) {
if (err){
console.log(err);
}else{
console.log(res.body);
}
}
);
}
// set max redirects:
function example10(){
httpreq.get('http://scobleizer.com/feed/',{
maxRedirects: 2, // default is 10
headers:{
'User-Agent': 'Magnet', //for some reason causes endless redirects on this site...
}},
function (err, res) {
if (err){
console.log(err);
}else{
console.log(res.body);
}
}
);
}
// set timeout
function example11(){
httpreq.get('http://localhost:3000/',{
timeout: (5 * 1000) // timeout in milliseconds
},
function (err, res) {
if (err){
console.log(err);
}else{
console.log(res.body);
}
}
);
}
// download file directly to disk:
function example12 () {
httpreq.download(
'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
__dirname + '/test.png'
, function (err, progress){
if (err) return console.log(err);
console.log(progress);
}, function (err, res){
if (err) return console.log(err);
console.log(res);
});
}