Introduction
Often time, as a developer, you are want to consume REST/JSON based APIs provided by third party applications or other teams in your enterprise. Now, if you see in order to continue with your development, you are actually dependent on those others team or third party applications. But can you really wait for them to complete their development before you start your own? In the agile world of development, it’s a complete no-no. It’s necessary that we proactively unblock ourselves and don’t get highly dependent on the other teams to provide services for us start consuming, developing etc. At the same time, we as consumers don’t want to create database, REST services ourselves. So, how do we unblock ourselves and break this deadlock? The answer is that we mock the the REST services for our purpose. There are several tools in market to help you with the REST service mock. In this tutorial, we will look at one such solution that helps us to easily mock REST services with json server and zero coding. As you complete this tutorial, you will realise there is absolutely zero coding. Actually, the official github page calls it so easy that we can spawn a full fledged fake API in less that 30 seconds.
Technology Stack
We use Node.js here.
We’ll use json-server which is a node module and it help you create fake REST API in a jiffy. Ensure that you have Node.js installed in your system (Windows, Linux, Mac anything). I am not going to cover installing Node.js in this tutorial. You may do a little google to check on how to install nodejs if you already don’t have it installed.
I’ll cover this tutorial in my Ubuntu system. However, if you are using any other OS, no issue; you can follow the same instructions.
So, let’s get started.
Install json-server node module
Json server is a node module and is available as NPM package. Hit the following single line command to install json-server in your system.
1 2 3 4 |
$ npm install -g json-server /usr/local/bin/json-server -> /usr/local/lib/node_modules/json-server/bin/index.js + json-server@0.14.0 added 230 packages from 130 contributors in 116.623s |
If you notice, the highlighted thrid line above, it tells you the version of json-server we just installed. Alternatively, you may run json-server -v command to check the version as well.
You may need to use sudo if you don’t have sufficient privilege to run this command.
Create mock Database
When you have REST service and want to mock CRUD operations, you must need a database. How else, you can demo! Well, these wonderful folks behind json-server have made that easy for us as well. You just mock the database. No need to create one for yourself. How do we do that?
Well, we’ll just create a JSON file called dbmockup.json in any directory of our choice and this file will contain all the data we want to expose to consumers. Here, we would like to expose orders data. So, let’s take a look at our content in dbmockup.json file. While you create the mock data, ensure that data must be an object; not array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
{ "Orders": [ { "id": 1234, "customer": { "firstName": "fname1", "lastName": "lname1" }, "orderLine": [ { "lineNumber": 1, "item": "laptop" }, { "lineNumber": 2, "item": "iphone" } ] }, { "id": 5678, "customer": { "firstName": "fname2", "lastName": "lname2" }, "orderLine": [ { "lineNumber": 1, "item": "macbook" }, { "lineNumber": 2, "item": "moto g phone" } ] }, { "id": 9012, "customer": { "firstName": "fname3", "lastName": "lname4" }, "orderLine": [ { "lineNumber": 1, "item": "chromecast" } ] } ] } |
Run json server
Now that you are ready with the mock, let’s run the json server from the same location where you have created the data. If you are in some other directory, ensure to provide the right path to the dbmockup.json file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ json-server --watch dbmockup.json \{^_^}/ hi! Loading dbmockup.json Done Resources http://localhost:3000/Orders Home http://localhost:3000 Type s + enter at any time to create a snapshot of the database Watching... |
Consume the REST services
-
HTTP GET all orders from browser
- Open a browser and hit the url http://localhost:3000/orders and you will see all the orders data listed in the browser.
-
HTTP GET specific Order from browser
- Open a browser and hit the url http://localhost:3000/orders/1234 and you will see the specific orders data listed in the browser.
-
HTTP POST (add a new order record)
- Use curl command to add a new Orders record
1234567891011121314$ curl --request POST --header "Content-Type: application/json" -d '{"id": 9024,"customer": {"firstName": "fname5","lastName": "lname6"}, "orderLine": [{"lineNumber": 1,"item": "Amazon fire stick"}]}' "http://localhost:3000/orders"{"id": 9024,"customer": {"firstName": "fname5","lastName": "lname6"},"orderLine": [{"lineNumber": 1,"item": "Amazon fire stick"}]} - Hit the http://localhost:3000/orders/ to check the addition of the new record we just inserted,
- Use curl command to add a new Orders record
-
HTTP PUT (modify an order record)
- Use curl command to update an existing Orders record with id 1234
1234567891011121314curl --request PUT --header "Content-Type: application/json" -d '{"id": 1234,"customer": {"firstName": "newname1","lastName": "new lname1"}, "orderLine": [{"lineNumber": 1,"item": "Amazon fire stick"}]}' "http://localhost:3000/orders/1234"{"id": 1234,"customer": {"firstName": "new fname1","lastName": "new lname1"},"orderLine": [{"lineNumber": 1,"item": "Amazon fire stick"}]} - Hit the http://localhost:3000/orders/1234 to check that the order record 1234 is updated with the new information.
- Use curl command to update an existing Orders record with id 1234
-
HTTP Delete
- Use curl command to delete an existing Orders record with id 1234
12curl --request DELETE "http://localhost:3000/orders/1234"{} - Hit the http://localhost:3000/orders/1234 to check that the order record 1234 is no more there.
- Use curl command to delete an existing Orders record with id 1234
Conclusion
Well, you have seen how easy it’s to mock REST/JSON services and how quickly you can unblock yourself instead of depending on a third party service provider to actually deliver you the service. Now, go develop 🙂