Introduction
Front-end development often relies on back-end API, and it is difficult to start the front-end work without the back-end API in place. Thats why we have Mock to deal with this issue. With Mock, front-end and back-end can cooperate concurrently. While back-end API is getting ready, front-end developers can utilize Mock to generate dummy data for development and debugging.
1. Mock Overview
Mock can automatically generate dummy data based on API or data structure definition, Mock rule configuration, and Mock expectation configuration. In addition, Mock provides flexibility to construct various APIs based on users’ needs.
In most scenarios, Apidog can generate user-friendly data by default.
How to Generate User-Friendly Mock Data Efficiently with Apidog?
- Apidog can generate mock rules based on data structure and data type in the API definition.
- Apidog has a built-in smart mock, which generates mock rules based on field name and field data type. For example, if a string field contains “image”, “time”, “city”, etc... in its name, Apidog will generate a url,string or name according to its field.
- Based on built-in mock rules, Apidog can automatically identify fields, such as image, avatar, username, cell phone number, URL, date, time, timestamp, email, province, city, address, and IP, to generate user-friendly dummy data.
- In addition to the built-in mock rules, users can also customize rules to meet various personalization needs. It supports string field matching using regular expressions or wildcards.
Below is an example of mock data generated by Apidog without any configuration:
The Difference Between Local and Cloud Mock Service
When you are running Apidog desktop app, you can use the local mock service.
When you are running Apidog web app, you can use the remote mock service.
2. URL Mock Request URL
The request method needs to be consistent with the API definition.
If your project ID is 18600, mock API ID is 89343, path is /user/123, and request method is POST, your mock URL is:
// Local Mock Address
POST http://127.0.0.1:3658/m1/18600-0-default/users/123
or
POST http://127.0.0.1:3658/m2/18600-0-default/89343
// Cloud Mock Address
POST https://mock.apidog.com/m1/18600-0-default/users/123
or
POST https://mock.apidog.com/m2/18600-0-default/89343
After defining the API and data structure, you can visit the URL above to visit the Mock data API without any additional manual configuration.
Mock URL
Local Mock:
- Path:
http://127.0.0.1:3658/m1/{project_id}-{version_id}-{service_id}/{api_path}
- Example:
http://127.0.0.1:3658/m1/18600-0-0/users/123
- ID:
http://127.0.0.1:3658/m2/{project_id}-{version_id}-{service_id}/{api_path}
- Example:
http://127.0.0.1:3658/m2/18600-0-0/84924
Cloud Mock
- Path:
https://mock.apidog.com/m1/{project_id}-{version_id}-{service_id}/{api_path}
- Example:
https://mock.apidog.com/m1/18600-0-0/users/123
- ID:
https://mock.apidog.com/m2/{project_id}-{version_id}-{service_id}/{api_path}
- Example:
https://mock.apidog.com/m2/18600-0-0/84924
- Project ID: You can view it in Project Settings.
- Version ID: The default version ID is
0
. Which is the main version. - Service ID: You will only need to specify the service ID when there are multiple services in the project. Otherwise, use default for the default service.
- When the mock service runs locally, the
URL IP
address is 127.0.0.1. If other machines need to access the mock data, simply change 127.0.0.1 tointranet
ip. If its still not accessible, please check whether the firewall is blocking port 3658 for the mock. - If there are multiple APIs with the same method and path, please use one of the following methods to specify API to avoid possible path conflicts:
- API Path method:Additional query parameter ( ?apidogApiId={api_id}).
- API ID mode: No additional setup is required.
- If the API path does not start with
/
, you will not be able to use API path method. Use API ID instead. - The mock service is turned on by default. No additional setup is required.
- The base URL in mock service is fixed. No manual modification is allowed.
Mock Feature in previous version
It is currently still backward compatible, but it will be deprecated in the future version.
- Path:
http://127.0.0.1:3658/mock/{project_id}/{api_path}
- Example:
http://127.0.0.1:3658/mock/18600/users/123
- ID:
http://127.0.0.1:3658/mock2/{project_id}/{api_path}
- Example:
[http://127.0.0.1:3658/mock2/18600/84924](http://127.0.0.1:3658/mock2/18600/84924)
Get API mock URL
Go to API details - View in Apidog to find the corresponding mock URLs.
3. Custom Mock Rule
Apidog also supports custom Mock Rule to meet your various business requirements.
Define mock rule in data structure definition
When defining schemas, you can set mock rules manually. We support Faker.js Data Placeholder Definition (DPD).
Data field advanced settings
You can also use the maximum, minimum, enumerated, pattern, and format values set in the advanced settings of the data field.
Advanced Mock
Advanced Mock is the most flexible mock. You can use custom schemas (not limited by the data structure of the API). The API can return different data based on the different request parameters.
Smart Mock
Smart Mock comes in when you do not provide mock rules for the fields in the return response (or data structure), the system will automatically generate realistic mock data without any manual configuration.
4. Mock Rule Priority
The priority of mock rule is as followed:
- Expectations in Advanced Mock of the API Detailed Settings (based on API parameters)
- Mock rules in the data structure.
- The Maximum, minimum, enumerated, pattern, and format values in the advanced settings of the data field.
- Custom mock rules defined in project settings - smart mock.
- Built-in mock rules in project settings - smart mock.
- The data type field in the data structure.
Automatically the system will default to mock the data structure of the first response in the API definition. If you want to mock other responses, you can get the mock URL of other responses in the Mock module of the API Detail Settings - View page
.