Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 14, 2020 02:51 pm GMT

Fake REST API with JSON and POSTMAN

You need back end data to connect with your front end? I will show you how to create fake JSON data with json-server and Postman.

I assume you have your project open, let's type this command to your terminal npm install -g json-server

After installation is done create a db.json file with some data.
Example:

{  "groceries": [    {      "id": 1,      "item": "bread",      "amount": "1"    },    {      "id": 2,      "item": "milk",      "amount": "3"    }   ]}

Start JSON Server json-server --watch db.json It should look like this in the browser.

Alt Text

I assume you have Postman installed, we will make a GET request.

Alt Text

Let's add one item with POST request.

Alt Text

Pros:

The dummy data is in your version control system
The simplest setup you can imagine
It supports the most used http verbs
It saves changes to your JSON source for POST, PUT, PATCH or DELETE requests.

Cons:

The payload requires a strict format (JSON)

If you are more for a video on how to do it, I highly recommend to watch this video

This is helpful to filter your data.

GET ALL GROCERIEShttp://localhost:3000/groceriesGET SINGLE GROCERYhttp://localhost:3000/grocery/1PAGINATION & LIMIThttp://localhost:3000/groceries?_page=1&_limit=2SORTINGhttp://localhost:3000/groceries?_sort=name&_order=ascFULL TEXT SEARCHhttp://localhost:3000/groceries?q=milk

Dummy data generators

https://github.com/Marak/faker.js
https://github.com/boo1ean/casual
https://github.com/chancejs/chancejs

I hope you will find this article helpful for your future projects.


Original Link: https://dev.to/tadea/fake-rest-api-with-json-and-postman-5gi8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To