DEV Community

Cover image for How to Mock complex APIs in TWO minutes
Ismail Kamil
Ismail Kamil

Posted on

How to Mock complex APIs in TWO minutes

Many developers love the solution I wrote in How to Mock an API in ONE minute. In the article, I introduced an approach to mock an API rapidly.

But what I wrote is only a tiny part of the tool. After a period of exploring, I found more valuable features to mock complex data faster.

Complex JSON

My last article introduced how to import a Swagger API into Apidog to mock some JSON data. Then you can have your mock data directly without any scripts.

But some fields are complicated; you can't mock them without any settings.

For example, an enum field has several optional values. It usually appears in status fields.

Or an array with a fixed length. It's often used in a table where each row has several attributes, and each page has 20 or 50 rows.

Or, an object refers to a schema. OpenAPI specification allows an API to refer to a schema as an object or part of an object.

Luckily, now we can quickly mock all these complex JSON in two minutes in Apidog.

Let's Mock it

There are three steps to mock the API.

  1. Import API definitions.
  2. Set specific fields.
  3. Get the mock URL.

So easy. Let's mock it!

Step 1 is the same as How to Mock an API in ONE minute. You just need an OpenAPI JSON or YAML and import it into Apidog.

Imported API definitions

Step 2. Set specific fields.

Enum values

In this sample API, the 'status' field has three optional values: available, pending, and sold. So I hope this field in my mocking data has only one of these values too.

Click 'More' in the 'status' field. You'll see an 'Enum' switch. Turn it on, and you'll be able to add enum values. After that, data of this field will be mocked according to the settings.

Field: More

Arrays with fixed length

PhotoUrls is an array field. I need it to have a fixed five items.

Click 'More' in the 'PhotoUrls' field. You'll see an 'Element Limit' setting. Set the minItems 5 and maxItems 5.

Field: More

Object refers to a schema

Apidog support schemas in OpenAPI specification. So you just need to select a Schema for an object, and Apidog will mock the object according to the schema.

For example, the 'Category' field.

Select schema

Step 3. Get the mock URL.

Click ‘Save', and switch to the 'API' tab. Copy the URL in the "Local Mock" part which starts with "127.0.0.1".

Copy the URL

Paste the URL into the browser.

That's it.

Mocking JSON

Notice these fields: photoURLs with fixed item quantity(5), while URLs are mocked automatically with available image URLs; status with enum values(sold, pending, or available), and category refers to a schema. The other fields, like city and createdAt are mocked automatically.

And if you reload the URL, the mock data refreshes.

Mocking JSON

JSON Schema

OpenAPI 3.0 data types are based on JSON Schema, which describes the data structure of APIs.

Apidog is compatible with OpenAPI. Length range, format, and pattern(regular expression) of strings, min and max value of integers, readonly/writeonly; all attributes in JSON Schema are supported.

Not only JSON but XML is also supported to mock. You can even set the tag names and prefixes of XML data.

Mocking XML

Based on JSON Schema, any complex mock data can be generated easily with a few mouse clicks. Still, you don't need to write any scripts.

Just enjoy it.

Download Apidog here, and wait for my next post to see more powerful mock features.

Top comments (2)

Collapse
 
vaibhav-saini123 profile image
vaibhav

we can also use Faux API platform, which gives us a lot of space to store values in APis and access it anytime we want, without any limits

faux-api.com/

Collapse
 
alexelziere profile image
Alex Elziere

well done