mockfly logo
Articles

>

How to Create a CRUD Using a Mock Server

How to Create a CRUD Using a Mock Server

What is a CRUD and How Can We Create a Mock API?

A CRUD stands for Create, Read, Update, and Delete. These are the typical operations performed on a resource in an API. We will create a mock API that allows us to perform these operations using different HTTP methods. For this, we will define the necessary routes of our mock server and the HTTP verbs needed to perform these actions.

We will see how to have dynamic routes and how to return JSON from our mock API. For this example, we will use a list of users as a resource.

Getting the List of Users from Our Mock Server

To get the list of users, we would normally make a GET request to the route /users. For this, we need to create a route in our mock server that responds to that request. We will do this very simply with Mockfly. We will create an endpoint called /users and instruct it to return a list of users like this:

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "[email protected]"
    },
    {
      "id": 2,
      "name": "Jane Smith",
      "email": "[email protected]"
    }
  ]
}

Now, every time we make a request to our mock server at the route /users, it will return this list of users. You can get the complete URL if you copy the link from the copy button in the route input in Mockfly.

We can make it a bit more dynamic if we want. For example, we can have it return a different name and email each time we make a request by changing the response JSON to something like this:

{
  "users": [
    {
      "id": 1,
      "name": "{{person.fullName}}",
      "email": "{{internet.email}}"
    },
    {
      "id": 2,
      "name": "{{person.fullName}}",
      "email": "{{internet.email}}"
    }
  ]
}

This allows us to make the lists returned by our mock API more realistic. This is possible because Mockfly uses Faker.js to generate random data, and if you want to know more, just visit the documentation.

Getting a User's Details from Our Mock API

Now we need to simulate the response of a real API to return a user's details. This can be done very simply using Mockfly as a mock server tool since it allows creating dynamic routes like /users/:id, where :id is a dynamic parameter.

That is, we will create an endpoint with the path /users/:id and instruct it to return a user's details with the ID we pass. For example, if we make a request to /users/1, it should return the details of a user with ID 1. For this, we need to tell our mock API to return a JSON like this:

{
  "id": "{{:id}}",
  "name": "{{person.fullName}}",
  "email": "{{internet.email}}",
  "city": "{{location.city}}"
}

This will ensure that every time we make a request to that endpoint on our mock API, it will return a user with the ID we provided and random name, email, and city.

Creating a New User on Our Mock Server

Obviously, a mock API is not connected to a database, but we can simulate the endpoint that would be used to create a user in this case. We simply need to create an endpoint with the path /users, and instead of a GET like before, it will be a POST. What can we respond with? We can respond with the user example we used before. We can even have it return a user with information we send in the request body. For example, if we send a JSON like this:

{
  "name": "John Doe",
  "email": "[email protected]",
  "city": "New York"
}

We could have it return a user with certain information that we send in case we need it by returning a JSON like this from our mock API:

{
  "id": 3,
  "name": "{{body.name}}",
  "email": "{{body.email}}",
  "city": "{{body.city}}"
}

This way, we are using the data received in the POST request in the response from our mock server.

Simulating the Deletion of a User in Our Mock API

Deleting is quite similar to creation and updating, as there's nothing to delete, but we can create an endpoint with the path /users/:id that uses the DELETE verb. This way, when we make a request to that route, we can return an empty JSON and status code 204 to indicate that the user has been deleted.

To do this, we simply create the route on our mock server, and all we need to do is change the status code from 200 to 204 and return an empty JSON.

Updating a User on Our Mock Server

Finally, to complete the CRUD, let's see how to "modify" a user in our mock API. As we've said, we're not actually modifying any user since it's a mock server and there are no real data, but we can create the endpoint with the route to /users/:id and a PUT verb. Thus, our mock API will handle the requests, and we can return a JSON similar to creation, which returns the data we send in the request:

{
  "id": 3,
  "name": "{{body.name}}",
  "email": "{{body.email}}",
  "city": "{{body.city}}"
}

Conclusion

With this, we would have a mock API that serves as a CRUD for a resource. In fact, we can create as many endpoints as we want and make it even more dynamic with Mockfly's rules and its different responses.