ReactJS REST API PUT Example

Introduction

Here I will show you ReactJS REST API PUT Example. In this tutorial I will show you how to update the existing resource via REST API PUT request using React JS framework. I will use ready made available REST API https://jsonplaceholder.typicode.com/posts for testing purpose. You can also create and deploy your own service using PHP, Codeigniter or Spring, Jersey etc. and call using React JS framework.

Related Posts:

Prerequisites

Knowledge of HTML and JavaScript, Create React JS Application, https://jsonplaceholder.typicode.com/posts

Project Directory

The name fo the project root directory is react-rest-put-api. Now you can delete everything from the src directory.

Controller Class

Create RestController.js file under src directory with below content. Notice that you need to import the required module or component such as import React from 'react'. I have also declared user array to fill with REST response data from https://jsonplaceholder.typicode.com/posts.

You can learn about componentDidMount() here at https://reactjs.org/docs/react-component.html.

The PUT URI updates an existing resource in the server and returns a response in the below format.

{
  id: , //id of the existing post -> user has input
  title: '', //title of the post to be updated -> user has input
  body: '', //body of the post to be updated -> user has input
  userId: //user id of the post to be updated -> who input
}

The source code of controller class is given below:

import React from 'react';
class RestController extends React.Component {
	constructor(props) {
		super(props);
		this.state = {user: []};
	}
	
	componentDidMount() {
		fetch('https://jsonplaceholder.typicode.com/posts/1', {
			method: 'PUT',
			body: JSON.stringify({
				id: 1,
				title: 'foo',
				body: 'bar',
				userId: 1
			}),
			headers: {
			  "Content-type": "application/json; charset=UTF-8"
			}
		}).then(response => {
						return response.json()
		}).then(json => {
			console.log(json)
			this.setState({
				user:json
			});
		})
	}
	render() {                            
		return (
			<div>
				<p><b>Resource updated in the server as shown below</b></p>
				<p>Id : {this.state.user.id}</p>
				<p>Title : {this.state.user.title}</p>
				<p>Body : {this.state.user.body}</p>
				<p>UserId : {this.state.user.userId}</p>
			</div>
		)
	}
}
export default RestController;

Export the RestController at the end of the RestController class so that we can use this class in other modules such as we have used it in below index.js file.

View File

Finally create index.js file under src directory to start up the application.
Here we have called <RestController/> component and writing the output of the REST API PUT response to the div id marked as root.

Open the file src/public/index.html, you will find a div with “root” id. Update the title in this file as “React – REST API PUT Example”.

import React from 'react';
import ReactDOM from 'react-dom';
import RestController from './RestController'

ReactDOM.render(<RestController />, document.getElementById('root'));

Testing the Application

Now execute the command npm start from command prompt and you will see the response data after creating the new resource in the server from https://jsonplaceholder.typicode.com/posts in browser at http://localhost:3000.

New Resource created in the server as shown below
Id : 1
Title : foo
Body : bar
UserId : 1

Final result on the browser as shown in the following image:

react js rest put api

Source Code

Download

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *