3

I have an API get requests in Postman that uses a data file of voucher codes to look up other information about the code, such as the name of the product the code is for. When using collection runner the voucher codes are passed incorrectly and the data is returned about the product.

For some reason, I'm unable to capture the data from the response body and link this into the next request.

1st get request has this in the body section:

{
    "dealId": 6490121,
    "voucherCode": "J87CM9-5PV33M",
    "productId": 520846,
    "productTitle": "A Book",
    "orderNumber": 23586548,
    "paymentMethod": "Braintree",
    "deliveryNotificationAvailable": true
}

I have this in the tests section to capture the values:

var jsonData = pm.response.json()    
pm.environment.set("dealId", jsonData.dealId);
pm.globals.set("productId", jsonData.productId);

when posting the next request in the body:

{
    "dealId":{{dealId}},
    "dealVoucherProductId": {{productId}},
    "voucherCode":"{{VoucherCode}}",
}

and pre-request scripts:

pm.environment.set("productId", "productId");
pm.globals.set("dealId", "dealId");

As you can see I've tried to use global and environmental variables both are not populating the next request body.

What am I missing?

n-verbitsky
  • 552
  • 2
  • 9
  • 20
99ajohnson
  • 83
  • 9
  • You don't need that set in your pre requests as it's not doing anything. You body in the next request is not valid JSON and all you're going to get through for `"{{VoucherCode}}"` is that exact string. Is there a reason why you have both `environment` and `global` variables set? – Danny Dainton Feb 05 '18 at 14:59

1 Answers1

0

This wouldn't set anything in those variables apart from the strings that you've added.

pm.environment.set("dealId", "dealId");
pm.globals.set("productId", "productId");

In order to capture the response data and set it in the variable you will need to add something like this to the first requests Tests tab:

var jsonData = pm.response.json()    
pm.environment.set("dealId", jsonData.dealId);
pm.globals.set("productId", jsonData.productId);

Depending on the response schema of the first request - This should set those values as the variables.

Postman

You can just use the {{dealId}} and {{productId}} where ever you need them after that.

If you're using a environment variable, ensure that you have created an file for those values to be set.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Thanks, although its now only running 1 iteration and saying there is an error, check the devtools. all i can see is thats its "undefined" – 99ajohnson Feb 05 '18 at 14:36
  • What is the response from the first request `jsonData.dealId` would only work for a very simple schema. If you post the response I can give the actual code you need to add. Can you post some of the request images from Postman too. – Danny Dainton Feb 05 '18 at 14:38
  • so this is the response body `{ "dealId": 6490121, "voucherCode": "J87CM9-5PV33M", "productId": 520846, "productTitle": "A book", "orderNumber": 23586548, "paymentMethod": "Braintree", "deliveryNotificationAvailable": true }` – 99ajohnson Feb 05 '18 at 14:41
  • Update the question with the details rather than in the comments. – Danny Dainton Feb 05 '18 at 14:49
  • great i can now see that captures the variables :) but when you run the collection in collection runner im getting this in the devtools ""Cannot read property 'toString' of undefined"" could this be because im trying to loop through multiple codes from the csv file? – 99ajohnson Feb 05 '18 at 15:05
  • As I have no idea of what you're sending or the way you have Postman configured adding a random error could mean anything. Please update the question with every piece of new information, including all images as this will bring what you're seeing to life. – Danny Dainton Feb 05 '18 at 15:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164566/discussion-between-danny-dainton-and-99ajohnson). – Danny Dainton Feb 05 '18 at 15:12