0

I’m quite new to postman (and coding) and was trying to find and piece together many snippets of scripts to make it work the way I want. What I want is very simple: I have a list of IDs that I want to make a POST in each of them, get one of the responseBody as a variable and do another POST. I think I’m close but I can’t manage to get it to work.

I’ve tried: Two POST request in the same Collection and running the collection. In the first request I have a POST to

https://APIADDRESS/?order_id{{orderid}}&contract[copy_order_data]=true

On the Pre-request Script tab:

var orderids = pm.environment.get(“orderids”);

if (!orderids) {
orderids = [“bc46bf79-2846-44ed-ac4d-78c77c92ccc8”,“81aacc33-1ade-41a3-b23e-06b03b526b8f”];
}

var currentOrderId = orderids.shift();
pm.environment.set(“orderid”, currentOrderId);
pm.environment.set(“orderids”, orderids);

On the Tests tab:

var orderids = pm.environment.get(“orderids”);

if (orderids && orderids.length > 0) {
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable(“invoice.id”, jsonData.invoice.id);
postman.setNextRequest(“Create invoice”);
} else {
postman.setNextRequest(null);

}

invoice.id is a environment variable populated with the response body of the first action/post and then using the variable on the second action/post.

And then the second request would be a POST to

https://APIADDRESS/invoices/{{invoice.id}}/finalize.json

Of course this doesn’t work. Either it doesn't run the second request in the collection or it doesn't do the loope to more than 1 ID on the list.

So I thought that putting the second POST inside the first one would solve it. But I had no luck.

Can please someone help me?

2 Answers2

0

I have tried mentioned use case with sample API's provided by POSTMAN. Can you try it?

First POST Method Request : https://postman-echo.com/post

Pre-request Script of first POST method

var orderids  = pm.environment.get("orderids");

if(!orderids ){
    orderids = ["bc46bf79-2846-44ed-ac4d-78c77c92ccc8","81aacc33-1ade-41a3-b23e-06b03b526b8f"];
}

var currentOrderId = orderids.shift();
pm.environment.set("orderid", currentOrderId);
pm.environment.set("orderids", orderids);

Tests Tab of first POST Method

var orderids = pm.environment.get("orderids");
if (orderids && orderids.length > 0) {
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("invoice.id", jsonData.headers.host);
postman.setNextRequest("Test1");
} else {
postman.setNextRequest(null);
}

Second POST Method Reqeust: https://postman-echo.com/post?key={{invoice.id}}

After executing the above collection it will set orederids and invoice.id value in environment variables and then it will call next POST Method.

Hope this will help you.

0

Thanks @HalfBloodPrince, from the Postman Echo it worked but in my case it doesn't :S What I manage to get it working was using a Json file as a list of Orderids. In that case I've separated all requests.

Request1 - https://APIADDRESS/?order_id{{orderid}}&contract[copy_order_data]=true

Tests tab:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("invoice.id", jsonData.invoice.id);

Request2 - https://APIADDRESS/invoices/{{invoice.id}}/finalize.json

That way everything is in a neat and organized way. Thanks