1const request = require("request");
2const url = "<a href="https://my-json-server.typicode.com/edurekaDemo/noderequest/db">https://my-json-server.typicode.com/edurekaDemo/noderequest/db</a>";
3request.get(url, (error, response, body) => {
4let json = JSON.parse(body);
5console.log(body);
6});
7
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});