Passing request parameters as variables to use in response body

Hello all.

I’m using the mock server, and my outbound API that I’m targeting at the server generates a unique transaction ID for each outbound call. I’m trying to take that unique parameter as well as take the first name and last name and pass it back in the response. This seems like something that should be pretty easy and straightforward, but I’m not finding anything after a few days of playing and sifting through the forums.

An example is:

incoming params = /home/list?TransactionID=123456789&FirstName=Joey&LastName=Sias

The trasaction ID will be unique every time. I need to return this value back with the response body. I would like the response body to work something like this:

“TransactionID”: “{{trans_id}}”,
“Name”: “{{first_name}} {{last_name}}”

Is this possible?

Thank you in advance for any help or tips. :slight_smile:

Hi @joey, Welcome to the community :partying_face:

Yes you can store the endpoint url and parse it to split and store the values as you need :slightly_smiling_face:

   var request = pm.request.toJSON().url.query;
   pm.environment.set("trans_id", request[0].value);
   pm.environment.set("first_name", request[1].value);
   pm.environment.set("last_name", request[2].value);

Can you please try this and let me know if this is your expectation :blush:

1 Like

Now that persist environment variable is no more available so this is not working that’s why I asked the other question :sob:

1 Like

@bpricilla That does not seem to work for me. The value returned with the response is “”
I’ve made this example really simple just to illustrate. And, I’m assuming you meant to use the pre-request script, or is there another area I should be using this?

Screen Shot 2021-04-17 at 5.40.04 PM

@joey, Yes under Pre-request script section.

I tried this way using Postman Echo:

I got the below value from GET request URL:

And sent them to the next Post request:

So, do you see the environment variable getting generated? BTW which Postman version you are using?

Yes @praveendvd it’s not listed under “Settings” :disappointed:

But can we try manually “Persist All” option under Environment.

Ya but it won’t be dynamic then , you cannot manipulate mockserver response from script .will raise a feature request or bug tomorrow

1 Like

Hi @bpricilla! Thank you for your help and for being patient here.

Sending “/get?param1=value1” seems to return these values back even without a pre-request script.

What I’m wanting to do is this:

  • set up a mock server in Postman
  • create an example that will return a 200 response which includes some of the request parameters in the response body that are captured from the inbound requests.
  • hit the mock server with a request from my personal api that I’m developing which sends a uniquie ID for each outbound request.
  • I’d like the mock server to return these parameters in it’s immediate 200 response body. (very much like is doing in your example)

Basically, how would I set up my postman mock server to do exactly what postman-echo just did.

I tried using the pre-request script as you’ve shown here and it only sends back “” as a value.

I’m using Postman 8.2.3

Hi @joey,
I’ve looked into trying to do this kind of dynamic response in a mock server several times in the last 12 months and concluded that this functionality is not currently supported. (maybe someone from the Postman team can confirm? @dannydainton ?). For dynamic mocking like you describe, you may want to look for service virtualisation tools rather than mocking tools until Postman extend this feature into their mocks.

1 Like

@joey - is this close to what you’re trying to do: #5 Transfer value from request to response? Requires updating an example in a pre-request script using pm.sendRequest() and the Postman API.

1 Like