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;
   console.log(request);
   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:

2 Likes

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 posman-echo.com 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? @danny-dainton ?). 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

I’m getting the error “Server did not recognize the value of HTTP Header” anyone please help us. Please correct me. Thanks in advance.

My script:

pm.sendRequest({
    url: "https://XXXXXXXtWS.asmx",
    method: "POST",
    header: {
        'Content-Type': 'text/xml',
        'SOAPAction': '""'
      },
    body: {
        mode:"raw",
        raw: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns2=\"https://www.logs.co.za/\"><SOAP-ENV:Header><ns2:Header></ns2:Header></SOAP-ENV:Header><SOAP-ENV:Body><ns3:login xmlns:ns3=\"https://www.logs.co.za/\" xmlns=\"\"><ns3:originId>XX</ns3:originId><ns3:username>XXX</ns3:username><ns3:password>XXXXXX</ns3:password></ns3:login></SOAP-ENV:Body></SOAP-ENV:Envelope> "
    }
},
function (err, res) {
    if (res.code === 200) {
        // console.log("Response - " + res.text());
        var responseJson = xml2Json(res.text());
        var sessionId = responseJson['soapenv:Envelope']['soapenv:Body'].loginResponse.result.sessionId;
        console.log("Session id - " + sessionId);
        pm.globals.set("session_id", sessionId);
    } else {
        console.log("Response - " + res.code + " " + res.reason());
        console.log("Response - " + res.text());
    }
}
);

 
I'm getting the following error in postman

<soap:Fault><faultcode>soap:Client</faultcode><faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

@cgopinath Shouldn’t this be a new question, as it doesn’t seem to relate to this topic.

However, the error is telling you what is wrong. You have a header called SoapAction, and it doesn’t have a value. I’m guessing the API will not accept this being null.

Hi, Still its not working I hv changed my SOAPACTION = “#POST” still im not getting the results. Thanks for your help.

@cgopinath

I don’t think that is a valid SoapAction.

You need to run the API specification to find out what is required here.

You could try “[ default ]” and see if that works though. Otherwise refer to documentation.