# How to use mockservers with a middleware service?

**URL:** <https://community.postman.com/t/how-to-use-mockservers-with-a-middleware-service/39647>\
**Category:** Help Hub\
**Tags:** mockservers\
**Created:** [September 30, 2022, 11:11am UTC](https://community.postman.com/t/how-to-use-mockservers-with-a-middleware-service/39647 "2022-09-30T11:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngiraud-aareon](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ngiraud-aareon/32/13356_2.png) [@ngiraud-aareon](https://community.postman.com/u/ngiraud-aareon)\
**Post date:** [September 30, 2022, 11:11am UTC](https://community.postman.com/t/how-to-use-mockservers-with-a-middleware-service/39647/1 "2022-09-30T11:11:15Z")

</div>

> **Archived topic — some details may be out of date.**  
> Mock servers built from saved collection examples are still supported. Postman now also has **code-based mocks** : JavaScript request handlers plus a `config.yaml`, stored as files in your Git repository, which you can run locally or deploy. Create one in the app, or use the Postman CLI to generate one from an OpenAPI spec, collection or dataset (`postman mock generate`) and run it (`postman mock run`). See [About mocks](https://learning.postman.com/docs/design-apis/mock-apis/mocks/overview) and [Create a mock](https://learning.postman.com/docs/design-apis/mock-apis/mocks/create-a-mock).  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) can build and run mocks for you.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

Hi everyone!

I am looking for a help as I am trying to provide different responses on same mock server calls. I tried with `x-mock-response-name`, `x-mock-response-id` and `x-mock-match-request-headers` but none of them are working on my system because I have not a classic “client-server” communication.

Let me explain my setup:

I have a **client** (let’s say local cURL or Postman), a **middleware** (an API I am developping) and a **server** (which I can’t edit at all). The **server** is the part I want to mock.

When the client requests the middleware, the middleware will trigger 2 different requests to the server (for instance, 1 for auth and 1 for the business logic) **by transfering a white list of HTTP Headers I can control** to the server.  
What I want to do is:

- when the **client** requests the **middleware** with some custom header “Custom-Header: A”, the **server** (mocked) gives the responses `R1` for the 1st request and `R2_A` for the 2nd request.
- when the **client** requests the **middleware** with some custom header “Custom-Header: B”, the **server** (mocked) gives the responses `R1` for the 1st request and `R2_B` for the 2nd request.

My problem with `x-mock-response-name` and `x-mock-response-id` is that, even if I can set `R2_A` or `R2_B`, on **client** headers (so that the middleware will transfer them to the server for the second request), Postman gives me an error because when the 1st request is sent, only `R1` is known so I have the `mockResponseNotFoundError` error.

I tried with `x-mock-match-request-headers`, but that’s basically the same issue: if on **client** side I am requesting the middleware with headers related to `R1` and `R2_A` for instance, the 1st request is not having a matching headers as the response would only have the `R1` header but not the `R2_A`.

Of course, even if I can control the white list of headers the middleware could transfer to the server, **I cannot choose which headers are transfered for each request**. The middle ware can only transfer to the server **all** the headers given that are matiching the whiltelist.

Is there any way to use something similar than `x-mock-response-name`, but if there are no responses matching with the given name, the default algorithm to select the example is executed rather than throwing an error? To me, this would be the easiest way to solve my situation.

Thank you a lot and please, do not hesitate to ask questions if I was unclear in my issue presentation 🙂

---

<div class="post-metadata">

**Author:** ![ngiraud-aareon](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ngiraud-aareon/32/13356_2.png) [@ngiraud-aareon](https://community.postman.com/u/ngiraud-aareon)\
**Post date:** [October 10, 2022, 7:41am UTC](https://community.postman.com/t/how-to-use-mockservers-with-a-middleware-service/39647/2 "2022-10-10T07:41:49Z")

</div>

In order to better explain the architecture I have, here is a little schema:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/a/7a9379e35d48526723c91fc8c042a344ef52d685.png)

What I want to mock are the servers on the **Server side** here. Here is my workflow:

1. The client (i.e. a curl request, Postman, …) calls my API with some data in the request body.
2. (a, b and c). My API is calling some servers with some data in the request body. Some of those data are inferred thanks to the data sent by the client, and other are generated by the API (like a timestamp for instance). As I expect the servers on the **Server side** are mocked, I get a fake response for each of those 3 requests.
3. My API builds and sends a response to the client thanks to the data fetched by the requests done on step 2.

Nothing too much complicated, eh? 😆

**My current problem is that I can’t choose which fake answer each mock servers must return on steps 2a., 2b. and 2c.**

Something else that is important is that we must consider that giving my example, requests 2b. and 2c. **are calling the exact same URL with the exact same HTTP Method: only the body differs**.

I tried with `x-mock-response-name` and `x-mock-response-id`: I can ask the client to send those headers as my API can only **transfer** some headers to the server side, but **can’t generate nor infer some**. The problem is that if I give the response-name or the response-id of the mock response for the request 2a., I will have a Postman error when the requests 2b. and 2c. will be sent (`mockResponseNotFoundError`).

I have the same problem with `x-mock-match-request-headers`, which I detailled in my previous post.

I can’t use `x-mock-response-code` as some requests (for instance 2b. and 2c.) will have the same  
expected response code so this header won’t filter the unexpected examples.

I can’t use `x-mock-match-request-body` as my API is sending some newly generated data to the server each time (like a current\_timestamp value, for instance), so even with the exact same request body sent by the client in step 1., the requests bodies for 2a., 2b., and 2c. will differ at every calls, so that the example can’t be found.

So, my question here is: is there any way to achieve this?  
Like I said, if Postman could simply follow its default algorithm instead of throwing an error if the `x-mock-response-name` doesn’t match, I could find a solution to this. In the meantime, I’m lost.

Thank you for your help!
