postman-use Postman’s simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Time:2023-9-12

Recently, the project is busy, the task more time is tight, resulting in a lot of back-end development tasks, can not be developed in a timely manner, but the front-end students have completed the development of the corresponding function, the need for front and back-end intermodulation to verify the API and some interactive issues; this can not be because of the progress of the back-end to affect the front-end of the completion of the work situation, therefore, the Postman mock data function is used, it is really good to use.

Introduction to Postman-Simulation Service

Setting up mockservers – Setting up mockservers: Postman has developed a new simulation service that enables teams to simulate back-end servers.Postman enables you to create simulated servers to assist in API development and testing. Simulated servers mimic the behavior of a real API server by accepting requests and returning responses. By adding a mock server to your collection and adding examples to your requests, you can simulate the behavior of a real API.


Front-end, back-end and API teams can now work in parallel, freeing up developers previously delayed by these dependencies. Now, let’s look at how Postman models the backend.

Using postman mock data

: The following screenshots show the function module may be different from yours, it should just be a version problem, find the corresponding function operation can be!

Creating a mock collection – Collection

Find a Collection

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Click on the + sign to create

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

You can see the collection I created
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Create request

As shown, I created a GET interface
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls


Create mock server

Selection set mock

Still here in the collection we created, see the three hollow points, click on them to see theMock collectionClick
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Placement MOCK

strike (on the keyboard)Mock collectionAfter that, the following screen will appear, give us a name for our mock server

  • Environment: Variables that store reused values, for example, if your url prefixes are uniform: https 8080/api, you can set environment variables that just simplify the operation and have no real impact, this will be covered below
  • Simulate a fixed network delay: network latency options, no special requirements and do not care

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Get the address of the mock server

After clicking create mock server, you will see the following page with aAddress of the mock serverThis mock server was created for us by postman on its own server. Whether we open postman or not, we can use this address to access the mock API directly.
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

This address is also not lost, as you can see in the following steps the mock server created:
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls


Create mock response

Now we have the request, we have the mock service, and we’re missing the response.

This step simply means that for request requests, you need to provide your own mock data.

In postman, this concept is called example, that is, each path/interface/resource you need to access, you can provide a , even if you can’t connect, you can use the example to know what kind of response will be returned by the path/interface/resource. this example is not just for mock and do, because postman can Generate API documentation, in the generation of documentation, this example will also be placed directly in the documentation, so that people can easily access the documentation to understand the interface.

Specific Steps:

1. Open the collection and select Request;

2. Create an example for the request;

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

3. Save the example;

Fill in the response data

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

There are a few points to note here:

  • Each request can have multiple examples.
  • Each example can be modified after it is created
  • The Mock server will only use the last example created.
  • Note the status code setting

Using mock server

Now we have the request request, the mock service, and the response content.

At this point, the mock server is no longer just an empty shell
It will provide a mock service for every request that creates an example.
The contents of the mock are the contents of the last example created.

We can verify this directly on the browser:

Authentication method: mock server address + api

postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls

Note that if it’s a POST request, odds are it will fail because browsers access it with GET by default


Environment

We talked about Environment earlier, so here is a brief explanation.

Mainly variables for storing reused values, for example, if your url prefix is uniformly: https 8080/api, you can set the environment variable, just to simplify operations

For example, I previously set up the
postman-use Postman's simulation service to simulate (mock) back-end data, complete the front-end simulation API calls


summarize

mock server is mounted on postman’s server, as long as there is a network, the front-end is able to work at home, without having to be subject to intranet limitations, with postman this feature, the front-end team and the back-end team can be completely in the agreement of the API interface, each to complete the development, without the need to receive the other side of the progress of the impact, but the interface, the agreement on the format of the data become especially important!

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]