Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

make a get request in node js

const axios = require('axios');

axios.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
  .then(response => {
    console.log(response.data.url);
    console.log(response.data.explanation);
  })
  .catch(error => {
    console.log(error);
  });
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

Get Request Node HTTP

const https = require('node:https');

https.get('https://encrypted.google.com/', (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });

}).on('error', (e) => {
  console.error(e);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to remove duplicate values in array of objects using javascript 
Javascript :: check date in between two dates nodejs 
Javascript :: javascript remove final newline from string 
Javascript :: is java and javascript the same 
Javascript :: factorial in javascript 
Javascript :: python iterate json file 
Javascript :: docker react 
Javascript :: jquery serialize with file upload 
Javascript :: react check if mobile or desktop 
Javascript :: getitem localstorage 
Javascript :: call laravel route js 
Javascript :: javascript .fill 
Javascript :: json.stringify formatting 
Javascript :: today date js 
Javascript :: react for loop in render 
Javascript :: get element class javascript 
Javascript :: uncheck a checkbox in javascript 
Javascript :: react forwardref 
Javascript :: boilerplate node js server 
Javascript :: window.location.search get parameters react 
Javascript :: how to preview a pdf document in react 
Javascript :: props vue 3 
Javascript :: call javascript function use array 
Javascript :: remove duplicate object from array javascript 
Javascript :: loop in object javascript 
Javascript :: chrome.tabs.query( 
Javascript :: convert string to number javascript 
Javascript :: the path argument must be of type string. received undefined react 
Javascript :: how to print object in JavaScript, Object print in JavaScript 
Javascript :: es6 create array with increasing number 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =