12

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.

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
  • I'm wondering that too, how does Postman Mock server actually map request URLs to `mockPath`? Does it assume all request URLs have the same domain and simply map the path to `mockPath`? What if I have different domains in a given collection, what `mockPath` would I use then? – Petrus K. Jun 13 '17 at 15:44
  • @PetrusK. - Currently mock does not distinguish between domains within a collection. It will deterministically return the first one in order if it hits the same http verb, path and `x-mock-response-code` for multiple examples. – Pratik Mandrekar Jun 14 '17 at 07:46
  • @PratikMandrekar Does that mean it will also deterministically return the first one in order if multiple folders contain the exact same request (I mean request URL)? We for example use the same request url (but with different body data) throughout folders. – Jim Aho Mar 02 '18 at 09:25
  • @JimAho - Yes, the response will be deterministic. If it returns one response for a URL it will return the same one the next time as well for the same collection. – Pratik Mandrekar Mar 05 '18 at 07:54

4 Answers4

9

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

MehranTM
  • 714
  • 9
  • 10
  • 3
    Does anyone know how to specify which example should be run if an api call is made by app code? For now looks like the first one that was created will run, but is there any way to change it? – wondersz1 Mar 11 '18 at 00:12
  • 1
    having trouble understanding how to add headers to a canned response – Alex Gordon Jun 01 '18 at 16:37
5

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.

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • I have a couple of folders inside my collection, and each folder can very well contain the same request as other folders. So assume I have two requests for `api.domain.com/api/foo` how does the mock know which request to pick? – Jim Aho Jun 14 '17 at 08:05
  • @JimAho What differentiates the requests between two folders? How do similar requests in two different folders behave differently? – Pratik Mandrekar Jun 14 '17 at 08:37
  • Nothing differentiates them, the reason why I have them duplicated is because when I use the postman runner feature and target a collection and folder, I want it to contain all neccessary requests. They do not behave differently, but only one of them might have example requests saved (which I understand is what the Mock Server uses). – Jim Aho Jun 14 '17 at 09:41
  • Yeah you shouldn't have a problem making the mock behave as expected with this arrangement. – Pratik Mandrekar Jun 14 '17 at 10:46
0

Also if you have several examples for the same query :

Mock request accept another optional header, x-mock-response-code, which specifies which integer response code your returned response should match. For example, 500 will return only a 500 response. If this header is not provided, the closest match of any response code will be returned.

Optional headers like x-mock-response-name or x-mock-response-id allow you to further specify the exact response you want by the name or by the uid of the saved example respectively.

Here's the documentation for more details.

Charles McD
  • 61
  • 1
  • 4
0

{{mockPath}} is simply the path for your request. You should start by adding an example for any of your requests.

Example:

Request: https://www.google.com/path/to/my/api

After adding your mock server, you can access your examples at:

https://{{mockId}}.mock.pstmn.io/path/to/my/api
Amr Hossam
  • 2,313
  • 1
  • 22
  • 23