What is RestAPI?

REpresentational State Transfer is an architectural style and not a protocol like SOAP for designing connected applications. It uses simple HTTP to make communication between machines possible. So, instead of using a URL for manipulating some user information, REST sends an HTTP request like GET, POST, DELETE, etc. to a URL to manipulate data.

For example, instead of making a GET request to a URL like /deleteuser?id=10, the request would be like DELETE /user/10

In REST architecture, a REST Server simply provides access to resources and the REST client accesses and presents the resources. And each resource is identified by URIs/ Global IDs. REST uses various representations to represent a resource like Text, JSON, and XML. JSON is now the most popular format being used in Web Services.

Alright, but why do I need mock RestAPI?

Because my friend every app and/or game nowadays either shows data which is on some web-server or manipulates some data which is…you get it right on some web-server. So during the development of your app, your back end team could have been behind deadline or in some case still discussing the sprint. You will need a mock RestAPI to test your web requests.

So let us start by sharpening the knives i.e downloading the software and preparing the system. What you will need is node.js setup, it requires no other configurations at all which means that you just need to install it and you’re are done; yes there are some commands that we have to fire but no big deal.

So download Node.js and install it on your system. I prefer to work in VSCode when coding in the node.js. Anyways let us start with some basics of the node.js.

I am hoping that up to this point you have installed the node.js on your system. At the time of writing this blog, I was using version 8.11.1 which is LTS version of node.js. To check the version of the node.js open your command prompt and type node -v hit enter.

If you are new to node.js or JavaScript then let me share the fact that there is a website with thousands of free java script packages that you can use to create your web apps so you don’t have to write everything from scratch and this amazing website is https://www.npmjs.com/, these packages are downloaded with the help of npm package manager with few simple commands.

We will be using JSON-Server to create mock RestAPI and later in this post, we will see how we can do CURD operations.

Navigate to the folder where you want to set up the JSON-Server. Shift+Right Click top open PowerShell in that folder – In my case, I have created a folder name “json-server” in my workspace. Type npm install -g json-serverand hit enter it will install the JSON-Server and you have a server ready.

Now we need some data on which we can do CURD operations to make sure that the web-requests are working as expected, for it we will create a mock JSON file, don’t worry here is a sample just download it and save it in your folder where you want to instantiate the server.

{
"marvel": [
{
"id": 1,
"superhero_name": "Deadpool",
"real_name": "Wade Wilson",
"powers": "Deadpool possesses a superhuman healing factor derived from that of the mutant Wolverine that allows him to regenerate damaged or destroyed areas of his cellular structure"
},
{
"id": 2,
"superhero_name": "Hulk",
"real_name": "Robert Bruce Banner",
"powers": "His capacity for physical strength is potentially limitless due to the fact that the Hulk's strength increases proportionally with his level of great emotional stress, anger in particular."
},
{
"id": 3,
"superhero_name": "Daredevil",
"real_name": "Matthew Michael Murdock",
"powers": "Daredevil possesses the strength, speed, agility, and endurance, not to mention acrobatic and gymnastic skills, of an Olympic-level athlete."
},
{
"id": 4,
"superhero_name": "Captain America",
"real_name": "Steven Rogers",
"powers": "Captain America represented the pinnacle of human physical perfection. He experienced a time when he was augmented to superhuman levels, but generally performed just below superhuman levels for most of his career."
}
]
}

Don’t judge me by the content of this JSON file, this is the best I can come up with, anyways let us host this file on our server and create our first RestAPI. To do that go back to your terminal where you have installed Json-Server and enter the command json-server marvel.json hit enter.

And that is it, you have a server with the RestAPI go ahead and test the URL it will show something like this –

Now all you need an app that will test this API, let us create a very simple Unity3D project which can perform CURD operation on the JSON file that we have created above.