forked from therebelslides/2015-unn-requests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.js
More file actions
52 lines (41 loc) · 1.16 KB
/
http.js
File metadata and controls
52 lines (41 loc) · 1.16 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
var http = require('http');
// Documentation: http://nodejs.org/api/http.html
// GET
var options = {
host: 'httpbin.org',
path: '/get?hello=world'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function(chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
console.log('GET response', str, typeof str);
});
}
http.request(options, callback).end();
// POST
var options = {
host: 'httpbin.org',
path: '/post',
//This is what changes the request to a POST request
method: 'POST',
// This isn't necessary for either request, just a demonstration that custom headers work
headers: {'custom': 'Custom Header Demo works'}
};
callback = function(response) {
var str = ''
response.on('data', function(chunk) {
str += chunk;
});
response.on('end', function() {
console.log('POST response',str, typeof str);
});
}
var req = http.request(options, callback);
//This is the data we are posting, it needs to be a string or a buffer
req.write("hello world!");
req.end();