{"id":1969,"date":"2021-02-09T12:00:00","date_gmt":"2021-02-09T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-make-rest-api-calls-in-react-get-post-put-delete\/"},"modified":"2026-07-05T03:25:21","modified_gmt":"2026-07-05T01:25:21","slug":"how-to-make-rest-api-calls-in-react-get-post-put-delete","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-make-rest-api-calls-in-react-get-post-put-delete\/","title":{"rendered":"How to Make REST API Calls in React \u2013 GET, POST, PUT, DELETE"},"content":{"rendered":"<p>In this tutorial, you will learn how to make REST API calls in React. We would cover this step by step with all the code snippets provided here. So let&#8217;s get started!<\/p>\n<p>We would make a REST API call to retrieve a list of posts from the url: <a href=\"https:\/\/jsonplaceholder.typicode.com\/posts\" target=\"_blank\" rel=\"noopener\">https:\/\/jsonplaceholder.typicode.com\/posts<\/a><\/p>\n<ol>\n<li><a href=\"#t1\">Making HTTP GET Request<\/a><\/li>\n<li><a href=\"#t2\">Making HTTP POST Request<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Making HTTP GET Request<\/strong><\/h4>\n<p>To make a POST request, we would need the axios package. So follow the steps below<\/p>\n<p><strong>Step 1<\/strong> &#8211; Install the axios package using the command<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm install axios --save\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong> &#8211; Create the PostList component. This would be used to display the list of posts. It would be a class component.<\/p>\n<p><strong>Step 3<\/strong> &#8211; Add the import statement to import axios to this component<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">import axios from 'axios'\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Add the constructor. Then add a state variable: postList. Initialize it to empty list<\/p>\n<p><strong>Step 5<\/strong> &#8211; You now need to write the code for the API call in the componentDidMount() lifecycle method. The code is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">componentDidMount(){\r\n    axios.get(<span style=\"background-color: #fff0f0;\">'https:\/\/jsonplaceholder.typicode.com\/posts'<\/span>)\r\n    .then(response <span style=\"color: #333333;\">=&gt;<\/span> {\r\n        <span style=\"color: #008800; font-weight: bold;\">this<\/span>.setState({postList<span style=\"color: #333333;\">:<\/span> response.data})\r\n    })\r\n    .<span style=\"color: #008800; font-weight: bold;\">catch<\/span>(error <span style=\"color: #333333;\">=&gt;<\/span> {\r\n        console.log(error)\r\n    })\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong> &#8211; In the render method, write the code to map over the postList and display the title and body of the posts. I&#8217;m placing the complete render() method below.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">render() {\r\n    <span style=\"color: #008800; font-weight: bold;\">const<\/span> {postList} <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span>.state\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> (\r\n        <span style=\"color: #333333;\">&lt;<\/span>div<span style=\"color: #333333;\">&gt;<\/span>\r\n            {\r\n                postsList.length <span style=\"color: #333333;\">?<\/span>\r\n                postList.map(post <span style=\"color: #333333;\">=&gt;<\/span> &lt;div key<span style=\"color: #333333;\">=<\/span>{post.id}<span style=\"color: #333333;\">&gt;<\/span>{post.title}<span style=\"color: #333333;\">&lt;<\/span>\/div&gt;)\r\n                <span style=\"color: #333333;\">:<\/span>\r\n                <span style=\"color: #008800; font-weight: bold;\">null<\/span>\r\n            }\r\n        &lt;\/div&gt;\r\n    )\r\n}\r\n<\/pre>\n<p><strong>Note<\/strong>: We destructure the postList and we also check if the returned list is empty<\/p>\n<p>At this point, you can test the application. You&#8217;ll see a list of post displayed.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Making a POST Request<\/strong><\/h4>\n<p>We would create a form with three fields: userId, title and body. These would be wrapped in a &lt;form&gt; tag with a submit button. Follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create the PostForm component and import the axios package. This would be a class component<\/p>\n<p><strong>Step 2<\/strong> &#8211; Add the constructor and add the three states initialized to empty string.<\/p>\n<p><strong>Step 3<\/strong> &#8211; In the render method, create the form with the three input fields. To make it easier for you, I&#8217;m providing the complete render method below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">render() {\r\n    const {userId, title, body} = this.state\r\n    return (\r\n        <span style=\"color: #007700;\">&lt;div&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;form<\/span> <span style=\"color: #0000cc;\">onSubmit=<\/span><span style=\"background-color: #fff0f0;\">{this.submitHandler}<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;div&gt;<\/span>\r\n                <span style=\"color: #007700;\">&lt;input<\/span> \r\n                <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">'text'<\/span> \r\n                <span style=\"color: #0000cc;\">name =<\/span><span style=\"background-color: #fff0f0;\">'userId'<\/span> \r\n                <span style=\"color: #0000cc;\">onChange=<\/span><span style=\"background-color: #fff0f0;\">{this.changeHandler}<\/span>\r\n                <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{userId}<\/span><span style=\"color: #007700;\">&gt;&lt;\/input&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;div&gt;<\/span>\r\n                <span style=\"color: #007700;\">&lt;input<\/span> \r\n                <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">'text'<\/span> \r\n                <span style=\"color: #0000cc;\">name =<\/span><span style=\"background-color: #fff0f0;\">'title'<\/span> \r\n                    <span style=\"color: #0000cc;\">onChange=<\/span><span style=\"background-color: #fff0f0;\">{this.changeHandler}<\/span>\r\n                <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{title}<\/span><span style=\"color: #007700;\">&gt;&lt;\/input&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;div&gt;<\/span>\r\n                <span style=\"color: #007700;\">&lt;input<\/span> \r\n                <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">'text'<\/span> \r\n                <span style=\"color: #0000cc;\">name =<\/span><span style=\"background-color: #fff0f0;\">'body'<\/span> \r\n                <span style=\"color: #0000cc;\">onChange=<\/span><span style=\"background-color: #fff0f0;\">{this.changeHandler}<\/span>\r\n                <span style=\"color: #0000cc;\">value=<\/span><span style=\"background-color: #fff0f0;\">{body}<\/span><span style=\"color: #007700;\">&gt;&lt;\/input&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;button<\/span> <span style=\"color: #0000cc;\">type=<\/span><span style=\"background-color: #fff0f0;\">'submit'<\/span><span style=\"color: #007700;\">&gt;<\/span>Submit Now<span style=\"color: #007700;\">&lt;\/button&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;\/form&gt;<\/span>\r\n        <span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n    )\r\n}\r\n<\/pre>\n<p>Note: Feel free to add labels to the input fields<\/p>\n<p><strong>Step 4<\/strong> &#8211; Add the changeHandler method to handle when the text input changes<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">changeHandler<span style=\"color: #333333;\">=<\/span>(e)  <span style=\"color: #333333;\">=&gt;<\/span>{\r\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span>.setState({\r\n        [e.target.name]<span style=\"color: #333333;\">:<\/span> e.target.value\r\n    })\r\n}\r\n<\/pre>\n<p>Note that we are using the same changeHandler for all the inputs. However, the name and value would vary depending on the input<\/p>\n<p><strong>Step 5<\/strong> &#8211; Add the submitHandler to submit the form.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">submitHandler <span style=\"color: #333333;\">=<\/span> (e) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    e.preventDefault()\r\n    axios.post(<span style=\"background-color: #fff0f0;\">'https:\/\/jsonplaceholder.typicode.com\/posts'<\/span>, <span style=\"color: #008800; font-weight: bold;\">this<\/span>.state)\r\n    .then(response <span style=\"color: #333333;\">=&gt;<\/span> {\r\n        console.log(response)\r\n    })\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong> &#8211; Add the PostForm to the App component and test the application. You will see the form displayed as shown below:<\/p>\n<figure id=\"attachment_14567\" aria-describedby=\"caption-attachment-14567\" style=\"width: 472px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-09-at-03.13.24.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-14567 size-full\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-09-at-03.13.24.png\" alt=\"React Input Form\" width=\"472\" height=\"150\" srcset=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-09-at-03.13.24.png 472w, https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2021\/02\/Screenshot-2021-02-09-at-03.13.24-300x95.png 300w\" sizes=\"auto, (max-width: 472px) 100vw, 472px\" \/><\/a><figcaption id=\"caption-attachment-14567\" class=\"wp-caption-text\">React Input Form<\/figcaption><\/figure>\n<p><strong>Step 7<\/strong> &#8211; Fill the form and submit. Then check the console. You will see the newly submitted record with a new id<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Homework<\/strong><\/p>\n<p>As a homework, please enhance the PostForm using Bootstrap. <a href=\"https:\/\/www.youtube.com\/watch?v=mWwnIdpJmkI\" target=\"_blank\" rel=\"noopener\">Find my steps on how to style with Bootstrap here<\/a>.<\/p>\n<p><a href=\"https:\/\/youtu.be\/qXvFaEkkZH8\" target=\"_blank\" rel=\"noopener\">See my video tutorial on How to Make REST API Calls.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to make REST API calls in React. We would cover this step by step with all the code &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[289],"tags":[],"class_list":["post-1969","post","type-post","status-publish","format-standard","hentry","category-rest-web-services"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1969"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1969\/revisions"}],"predecessor-version":[{"id":2137,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1969\/revisions\/2137"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}