Node JS Make HTTP Put Request Example

By Hardik Savani May 4, 2021 Category : Node JS

Hi,

Are you looking for example of node js http put request. Here you will learn how to make http put request nodejs. This post will give you simple example of node js axios put request example. This article goes in detailed on http put request body nodejs.

i will give you two examples, using axios and request npm package for male put/patch http request using node js. let's see both example with output as bellow:

Example 1: HTTP Put Request using Axios

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install axios --save

server.js

const axios = require('axios');

const data = {

name: 'Hardik Savani',

job: 'Blog Writer'

};

axios.put('https://reqres.in/api/users/2', data)

.then((res) => {

console.log(`Status: ${res.status}`);

console.log('Body: ', res.data);

}).catch((err) => {

console.error(err);

});

Run App

node server.js

Output

Status: 200

Body: {

name: 'Hardik Savani',

job: 'Blog Writer',

updatedAt: '2021-05-03T12:59:03.800Z'

}

Example 2: HTTP Put Request using Request

Create Node App:

mkdir my-request-app

cd my-request-app

npm init

Install Axios:

npm install request --save

server.js

const request = require('request');

const options = {

url: 'https://reqres.in/api/users/2',

json: true,

body: {

name: 'Hardik Savani',

job: 'Blog Writer'

}

};

request.put(options, (err, res, body) => {

if (err) {

return console.log(err);

}

console.log(`Status: ${res.statusCode}`);

console.log(body);

});

Run App

node server.js

Output

Status: 200

{

name: 'Hardik Savani',

job: 'Blog Writer',

updatedAt: '2021-05-03T13:00:22.506Z'

}

I hope it can help you...

Tags :
Shares