Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node http post

const https = require('https');

const data = JSON.stringify({
    name: 'John Doe',
    job: 'Content Writer'
});

const options = {
    hostname: 'reqres.in',
    path: '/api/users',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'Content-Length': data.length
    }
};


const req = https.request(options, (res) => {
    let data = '';

    console.log('Status Code:', res.statusCode);

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

    res.on('end', () => {
        console.log('Body: ', JSON.parse(data));
    });

}).on("error", (err) => {
    console.log("Error: ", err.message);
});

req.write(data);
req.end();
Comment

nodejs request post

const axios = require('axios');

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

    const users = res.data;

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

PREVIOUS NEXT
Code Example
Javascript :: jquery disable form element 
Javascript :: match date regex 
Javascript :: jquery test div exists 
Javascript :: angular httpclient query params not working 
Javascript :: regex for yyyy-mm-dd 
Javascript :: next day javascript 
Javascript :: line break in react 
Javascript :: javascript get current week number 
Javascript :: React Native BUILD FAILED on run-ios 
Javascript :: json server npm 
Javascript :: how to concurrently run angular and node 
Javascript :: filter special characters javascript 
Javascript :: get last item in map javascript 
Javascript :: js string to date 
Javascript :: react native keystore 
Javascript :: items from first array that are not in the second array javascript 
Javascript :: react-native curved view 
Javascript :: why use currying 
Javascript :: sweetalert angular 8 
Javascript :: axios pass params 
Javascript :: get attribute href 
Javascript :: mongoose connect to URL of atals 
Javascript :: javascript redirect page 
Javascript :: redirect to html page in javascript 
Javascript :: get class count in jquery 
Javascript :: datetime to date in js 
Javascript :: how to show only time in hours and minutes only in javascript 
Javascript :: json parse array local storage 
Javascript :: ExpressionChangedAfterItHasBeenCheckedError: 
Javascript :: uppercase first letter of each word javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =