1let request = require('request')
2
3const formData = {
4 // Pass a simple key-value pair
5 my_field: 'my_value',
6 // Pass data via Buffers
7 my_buffer: Buffer.from([1, 2, 3]),
8 // Pass data via Streams
9 my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
10 // Pass multiple values /w an Array
11 attachments: [
12 fs.createReadStream(__dirname + '/attachment1.jpg'),
13 fs.createReadStream(__dirname + '/attachment2.jpg')
14 ],
15 // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
16 // Use case: for some types of streams, you'll need to provide "file"-related information manually.
17 // See the `form-data` README for more information about options: https://github.com/form-data/form-data
18 custom_file: {
19 value: fs.createReadStream('/dev/urandom'),
20 options: {
21 filename: 'topsecret.jpg',
22 contentType: 'image/jpeg'
23 }
24 }
25};
26request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
27 if (err) {
28 return console.error('upload failed:', err);
29 }
30 console.log('Upload successful! Server responded with:', body);
31});
1const request = require('request');
2request('http://www.google.com', function (error, response, body) {
3 console.error('error:', error); // Print the error if one occurred
4 console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
5 console.log('body:', body); // Print the HTML for the Google homepage.
6});
1request .get('http://google.com/img.png')
2 .on('response', function(response) {
3 console.log(response.statusCode) // 200
4 console.log(response.headers['content-type']) // 'image/png' })
5 .pipe(request.put('http://mysite.com/img.png'))