Understanding mock server

I would like to understand better how mock servers work in Postman.

I think I know how the mock server works in Postman, however I am very new to APIs and mock servers so I am not sure. As I understand it, when you make a request with the mock server it doesn’t actually interact with your API, it just uses an algorithm to determine which mock data to give back as the response depending on the request parameters, headers, and body. Is this correct? Or does it still interact with the api?

The reason that I think this is because, when you make a request with the mock server, it doesn’t use the api url like normal. It instead replaces it with the mock url.

For example, if I normally make a request this way:

Any help understanding how Postman’s mock servers function would be greatly appreciated.
Thanks!

A mock mimics your API.

The difference between a Mock and Stub is that Mock can return difference responses and is a bit more interactive, over a Stub which usually just returns the same response each time.

You don’t have to use Postman for this, and there are tons of Mock servers that you can install locally and call instead.

Instead of calling your API endpoint, you will call the mock server API endpoint.

The Postman Mock server is fairly easy to use, but is run in the Postman Cloud, and does consume resources there. So you need to consider if the mock is returning confidential data, etc.

Within Postman, you can save requests as examples (or generate your own example response from scratch).

Once you have your examples, you can then configure the Mock server within that collection.
The mock server usually has your collection ID as part of the URL.
Postman will use the examples to determine what data is sent back.

Understanding example matching | Postman Learning Center

Once the mock is setup, you can add examples at any time.

It’s a fairly quick way of setting up a Mock instead of using a local server or another mock service.

In your requests, you would normally parameterize the hostname part of the URL using a collection or environment variable, leaving the rest of the URL the same. (Paths, parameters, etc).

To change to the mock should then be as easy as updating the current value in the hostname variable.

2 Likes

Thank you so much for your response mdjones!

I have a follow-up question, if that’s alright: If I change the code in my api, would the mock server in postman reflect this? Or does it not interact with my api at all?

For example, lets say I have an api (https://myApi.com), and I have created a mock server based off of that api in postman.

Lets say that I have an endpoint called helloWorld, and when I make a request with https://myApi.com/helloWorld, the response it gives back is “Hello World!”

Lets say that I have saved this response as an example in my mock server, so that when I call {{mockUrl}}/helloWorld it gives me back “Hello World!”

Now, lets say that I change the code in myApi so that calling the helloWorld endpoint no longer returns “Hello World!” but rather returns “Good Morning!”. Would my mock server now return “Good Morning!”, or would it still return “Hello World!”? My assumption is that it would still return “Hello World!”.

The reason that I am asking this is because my team is considering putting postman tests on our CI/CD pipeline that test one of our APIs. We were thinking of putting tests that use the mock server url on the pipeline instead of tests that use the actual API’s url.
However, I was thinking that this might not be effective because, if we make changes to our API, the mock server tests will always return the same response (at least I think they will), so these tests won’t actually help us to know whether our api is working correctly or not.

So, is that true? Will the mock server tests always return the same response, even if I change the api code?

I know that postman mock servers use a matching algorithm that matches a request with the example that most closely matches it. Is that all that it does? Or does it doing something else to interact with the api?

It doesn’t interact with your API at all apart from when you want to save examples based off real responses.

One of the uses for mocks is for early on in development when the API may not even exist. So you mock what you will expect the API to eventually return (based on an already agreed API specification), to allow the front end developers to get on with their part.

If your API doesn’t exist yet, then the examples will have to be created from scratch.

If you change what your API accepts or returns, then you may need to update the examples in Postman to match or it will just continue to return what was previously saved in the examples.

The point is to get the mock to return responses of what your API should return.
If you change your API, then there is a good chance you will need to update the examples in the mock to meet the new specification.

The mock will always return the responses from the examples. If you send the same request, it should always give you the same response. However Postman does support some flexibility in that area that allows you to change certain parameters and have them echoed back to you but usually, yes it will always return the same data.

Postman knows nothing about the inner working of your API or its code.

Mocks will never tell you if your API is working or not. It just mimics what should be sent back.

You might have the API mocked in the DEV pipeline, but it should be connecting to the real API in SIT, PRE-PROD, etc.