1

In Postman, I wish to create a mock server that contains a request with 3 different examples of a response, each one associated to a HTTP code, so that on three successive calls, that request returns the mock responses in the given order. Precisely:

  • First call returns 202;
  • Second call returns 208;
  • Third call returns 200.

From what I have gathered (such as here: https://community.postman.com/t/how-to-select-which-example-is-used-by-a-mock-server-request/15679 ) a mock response can be picked using the x-mock-response-id header. This I have succeeded in doing when sending the request from Postman (using a pre-request script), but it does not work when the request is called from my script. How could I fix that in Postman? Is it only possible?

Just in case, said pre-request script is:

nthCall = pm.environment.get("nthCall");
if ( nthCall == null || nthCall > 2 ) {
    nthCall = 0;
}

const CALL_MOCK_ID_1 = "16343539-67f13a78-2a17-48d4-ba43-8489815b4eb0";
const CALL_MOCK_ID_2 = "16343539-5290f2e1-b81a-4d71-9548-2b06ff4f2461";

var callMockId = null;
if ( nthCall == 0 ) {
    callMockId = CALL_MOCK_ID_1;
} else if ( nthCall == 1 ) {
    callMockId = CALL_MOCK_ID_2;
}

pm.environment.set("callMockId", callMockId);

nthCall++;
pm.environment.set("nthCall", nthCall);
Bonus question

Also, I wondered if there was a way to indicate in a URL of a mock request that it should accept any UUID, as in: https://abcdef01-2345-6789-abcd-ef0123456789.mock.pstmn.io/company/<uuid>, so that when my script calls this URL, it returns the corresponding example whatever the UUID.

Elg
  • 31
  • 5
  • you should specify the header , please add the script that you are trying to call the mock , make sure you have he header set – PDHide Dec 07 '21 at 02:45
  • The point is, I am trying not to edit my script just to fit the requirements of Postman. If it is necessary, I will probably end up doing just that - but my question here was precisely if and how I could get Postman to do it, by making it use my script before sending a reponse, for example. – Elg Dec 07 '21 at 09:11
  • its hard to understand what you are trying to do here, why you need that pre request script ? you are just setting two environment variable. I am not sure where it is used – PDHide Dec 07 '21 at 09:28

0 Answers0