Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Postman Mock Server

Tags:

api

postman

I have followed the guide here to create a postman mock for a postman collection. The mock seem to be successfully created, but I have no idea how to use the mock service.

I've been given a url for the mock, but how do I specify one of my requests? If I issue a GET request to https://{{mockid}}.mock.pstmn.io I get the following response:

{
    "error": {
        "name": "mockRequestNotFoundError",
        "message": "We were unable to find any matching requests for the mock path (i.e. undefined) in your collection."
    }
}

According to the same guide mentioned above the following url to "run the mock" https://{{mockId}}.mock.pstmn.io/{{mockPath}} but what exactly is mockPath?

Within my collection I have plenty of folders, and inside one of these folders I have a request with an example response. How do I access this example response through the mock? Thanks for all help in advance!

Here's the Postman Pro API, which doesnt mention a lot more than just creating reading mocks.

like image 459
Jim Aho Avatar asked Jun 13 '17 07:06

Jim Aho


Video Answer


2 Answers

I had the same issue seeing an irrelevant error but finally I found the solution. Unfortunately I cannot find a reference in Postman website. But here is my solution:

When you create a Mock server you define your first request (like GET api/v1/about). So the Mock server will be created but even when you obtain your API key and put it in the header of request (as x-api-key) it still returns an error. It doesn't make sense but it turned out that defining the request is not enough. For me it only started returning a response when I added an Example for the request.

So I suggest for each request that you create, also create at least one example. The request you send will be matched with the examples you have created and the matched response will be returned. You can define body, headers and the HTTP status code of the example response..

I have no Pro Postman subscription and it worked for me using my free subscription.

Menu for adding an example or selecting one of them for editing: enter image description here

UI for defining the example (See body, headers and status) : enter image description here

How to go back to the request page: enter image description here

Here is the correct reply I get based on my example: enter image description here

like image 78
MehranTM Avatar answered Oct 22 '22 06:10

MehranTM


If you request in the example is a GET on api.domain.com/api/foo then the mockPath is /api/foo and your mock endpoint is a GET call to https://{{mockid}}.mock.pstmn.io/api/foo.

The HTTP request methods and the the pathname as shown in the image below constitute a mock.

breaking down a url

For ease of use the mock server is designed to be used on top of collections. The request in the examples is used as is along with response attached to it. The name of the folder or collection is not a part of the pathname and is not factored in anywhere when using a mock. Mocking a collection means mocking all the examples in within your collection. An example is a tuple of request and response.

An optional response status code if specified lets you fetch the appropriate response for the same path. This can be specified with the x-mock-response-code header. So passing x-mock-response-code as 404 will return the example that matches the pathname and has a response with status code of 404.

Currently if there are examples with the same path but different domains, and mock is unable to distinguish between them it will deterministically return the first one.

like image 5
Pratik Mandrekar Avatar answered Oct 22 '22 05:10

Pratik Mandrekar