Start running Rest API on your local without writing any code

Below tutorial will show how to start a demo Rest API on a JSON Server without writing a single piece of code. It will also show how you can test the API using Rest-Assured.

Steps:

1. Download Node.js on your machine. (https://nodejs.org/en/download/)

2. Install Node.js

3. Open Command Prompt and check that npm and node is installed

node -v
npm -v

4. Install JSON Server

npm install -g json-server

5. Create a db.json file with some data

{
  "posts": [
    {
      "id": 1,
      "title": "json-server-1",
      "author": "qascript"
    },
    {
      "id": 2,
      "title": "json-server-2",
      "author": "qascript"
    }
  ],
  "comments": [
    {
      "id": 1,
      "body": "test1",
      "postId": 1
    },
    {
      "id": 2,
      "body": "test2",
      "postId": 2
    }
  ],
  "profile": {
    "name": "qascript"
  }
}

6. Start JSON Server

json-server --watch db.json

7. Open http://localhost:3000/posts in any browser to view JSON response

8. Write the following code to test a POST request on the demo Rest API running on the localhost.

@Test
public void PostRequestLocalJsonServer(){
       String payload = "{\n" +
               "      \"id\": 3,\n" +
               "      \"title\": \"json-server-3\",\n" +
               "      \"author\": \"qascript\"\n" +
               "    }";

       given().contentType(ContentType.JSON)
               .body(payload)
               .post("http://localhost:3000/posts")
               .then().statusCode(201)
               .log().body();
   }

9. Again open the URL(https://localhost:3000/posts) and notice that a new post is added to the api.

Reference: https://github.com/typicode/json-server

 

Share
Bijan Patel

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

1 Response

  1. RAMAGANO712 says:

    Thank you!!1

Leave a Reply

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

error: Content is protected !!