Mock REQUEST bodies automatically to accelerate API debugging
Tags: webdev, tutorial, beginners, productivity
Published: 2022-11-28 10:18:59

When developing APIs, debugging is always a boring problem. 

Postman can help send requests, but when the API body is complicated, constructing a JSON body would be difficult. Junit also works, but all requests must be constructed yourself in testing classes.

Complex bodies make many backends abandon unit tests and output an untested API to frontend developers and QA engineers. 

The above case demonstrates that the code quality declines without a good tool. 

And now, I found that [a tool named Apidog](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock_request_body) can generate the request body automatically when debugging. The tool can construct a correct request JSON itself.

## How to mock it

This approach works for API-first teams. APIs are designed before developing, so while debugging, you have an API specification.

1.Import the API specifications into Apidog. ([How to?](https://dev.to/ismailkamil/how-to-mock-an-api-in-one-minute-eip/))
2.Open an API, and make sure the request has a JSON/XML body specification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8depoegey0lem5hc185.png)

3.Switch to the "Run" tab, and click the "Generate Automatically" button.

OK. You've got a mock JSON according to the request specification!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9elinaoj7q2qtf3yfxc5.png)

And now click "send" to send the request like what we did in Postman, but more efficiently. Most features in Postman are also supported in Apidog.

Our team have used Apidog to debug APIs instead of Postman for weeks.

## What's more

Notice the mock data. It was generated automatically according to the field name. A field named "city" are mocked with a city name, a field named photoUrl is mocked with a photo URL, and all mock data follows JSON schema specified in the API. Just like what I wrote in [How to mock complex JSON](https://dev.to/ismailkamil/how-to-mock-complex-json-in-two-minutes-2m9n).

And that's not the end.

Hover on the arrow beside "Generate Automatically", you'll see three menu items.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaoiftrapox2sjkb02oh.png)

"Default value first" means the mock JSON would be filled by the default value specified in each field of the API first. "Example value first" means the mock JSON would be loaded by the example request JSON first. But what is "Refresh when sending request"?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjlt5ln9op9zeue7oj3t.png)

It's just the literal meaning. Each time the request is sent, the body is generated again.

Interesting, isn't it?

## Customize it

Until now, everything looks great.

But what if some of my fields are not expected to change? How can I make some fields fixed and some mocked?

Click the "Dynamic Value" button, and you'll find a new world.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0xe2jlba4tt8c6yl640.png)

All Faker.js syntaxes are supported, and you can select any of them directly. You can even select a function to mutate the mock value like md5, substr or base64 etc.
Then it will be inserted into the request body, just like this.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y85l19nu6nn5ttcasdz.png)

You can decide which field is fixed and which is mocked by Faker.js. When sent, all faker fields will change to the actual data below. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47yomatmtqnsti0hcnxc.png)

That's it. And it changes each time sending.

Apidog is a great tool that makes our team build APIs faster. I like it.

[Download Apidog here](http://www.apidog.com/?utm_source=dev_to&utm_medium=IsmailKamil&utm_content=Mock_request_body).