Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs request api

const https = require('https');

https.get('https://jsonplaceholder.typicode.com/users', res => {
  let data = [];
  const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
  console.log('Status Code:', res.statusCode);
  console.log('Date in Response header:', headerDate);

  res.on('data', chunk => {
    data.push(chunk);
  });

  res.on('end', () => {
    console.log('Response ended: ');
    const users = JSON.parse(Buffer.concat(data).toString());

    for(user of users) {
      console.log(`Got user with id: ${user.id}, name: ${user.name}`);
    }
  });
}).on('error', err => {
  console.log('Error: ', err.message);
});
Comment

nodejs request

var request = require('request');
var options = {
    method: 'POST',
    uri: 'https://onfleet.com/api/v2/workers',
    body: '{"name":"Marco Emery","image":"http://cdn3.addy.co/images/marco.png","phone":"415-342-0112","teams":["0pgyktD5f3RpV3gfGZn9HPIt"],"vehicle":{"type":"CAR","description":"Tesla Model 3","licensePlate":"CA 2LOV733","color":"purple"}}',
    headers: {
        'Authorization': 'Basic ' + new Buffer("c64f80ba83d7cfce8ae74f51e263ce93:").toString('base64')
    }
};
request(options, function(error, response, body) {
    console.log(body);
});
Comment

node js http request

// Importing https module
const http = require('http');
  
// Setting the configuration for
// the request
const options = {
    hostname: 'jsonplaceholder.typicode.com',
    path: '/posts',
    method: 'GET'
};
    
// Sending the request
const req = http.request(options, (res) => {
    let data = ''
     
    res.on('data', (chunk) => {
        data += chunk;
    });
    
    // Ending the response 
    res.on('end', () => {
        console.log('Body:', JSON.parse(data))
    });
       
}).on("error", (err) => {
    console.log("Error: ", err)
}).end()
Comment

node js do request

let request = require('request')

const formData = {
  // Pass a simple key-value pair
  my_field: 'my_value',
  // Pass data via Buffers
  my_buffer: Buffer.from([1, 2, 3]),
  // Pass data via Streams
  my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
  // Pass multiple values /w an Array
  attachments: [
    fs.createReadStream(__dirname + '/attachment1.jpg'),
    fs.createReadStream(__dirname + '/attachment2.jpg')
  ],
  // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
  // Use case: for some types of streams, you'll need to provide "file"-related information manually.
  // See the `form-data` README for more information about options: https://github.com/form-data/form-data
  custom_file: {
    value:  fs.createReadStream('/dev/urandom'),
    options: {
      filename: 'topsecret.jpg',
      contentType: 'image/jpeg'
    }
  }
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
  console.log('Upload successful!  Server responded with:', body);
});
Comment

http request node.js

var http = require('http');

//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
  host: 'www.random.org',
  path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};

callback = function(response) {
  var str = '';

  //another chunk of data has been received, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been received, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}

http.request(options, callback).end();
Comment

PREVIOUS NEXT
Code Example
Javascript :: open a particular slide on click button in owl carousel 
Javascript :: run nextjs in separate port 
Javascript :: angular right click 
Javascript :: get cursor position in contenteditable div 
Javascript :: //disable-linter-line 
Javascript :: chartjs disable animation 
Javascript :: jquery form validation plugin callback function 
Javascript :: How To Set Opacity of a View In React Native 
Javascript :: get last item in array 
Javascript :: maximum sum subarray javascript 
Javascript :: node redisjson get properties of array object 
Javascript :: socket.io reconnect example 
Javascript :: javascript create an array of range between two numbers 
Javascript :: find array with children javascript 
Javascript :: to capital case javascript 
Javascript :: hex to rgba in js 
Javascript :: check if type is blob javascript 
Javascript :: Vue use props in style 
Javascript :: jquery input value change event not working 
Javascript :: how to convert milliseconds to time in javascript 
Javascript :: jquery remove child 1 elemtn 
Javascript :: For loop sum in javascript 
Javascript :: rust read json file 
Javascript :: textField input font color React Material UI 
Javascript :: javascript append to array 
Javascript :: react native scrollview map 
Javascript :: timestamp to date javascript 
Javascript :: how to reset settimeout in javascript 
Javascript :: iseet jquery 
Javascript :: declare empty object javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =