2

I want to orchestrate two requests in Postman. The first response will give me a variable. I save this id to a global variable id. In Postman this variable is usually accessible via {{id}}.

Then I send a second request with this id (like GET foo.bar/{{id}}). Now I want to check, if the id is in the result as well.

This is what I tried in the test code:

    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql({{id}});

where idis the variable from the first response (e.g. 72b302bf297a228a75730123efef7c41).

The response for the second request looks sth. like this:

{
    "id": "72b302bf297a228a75730123efef7c41"
}

Here some examples which did not work either:

    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql("{{id}}");
    var jsonData = pm.response.json();
    var myId = {{id}};
    pm.expect(jsonData.id).to.eql(myId);

My excpectation is, that the test will be positive and the `id from the request will be found in the response.

Do you have an idea how to solve this problem?

Thanks for the help.

1 Answers1

1

The {{...}} syntax cannot be used like that, in the sandbox environment, you would need to access it this way.

pm.expect(jsonData.id).to.eql(pm.globals.get('id'))

The test syntax would be:

pm.test("IDs equal?", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.id).to.eql(pm.globals.get('id'))
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Hi Danny, thanks for the help. I tried ```pm.test("IDs equal?", function () { var jsonData = pm.response.json(); pm.expect(jsonData.id).to.eql('{{id}}'); });``` The programm does not allow to cast the id variable before, so that `...to.eql('id')` is not possible (as far as I know). Here is the surrounding code: ```pm.test("IDs equal?", function () { var jsonData = pm.response.json(); pm.expect(jsonData.id).to.eql('{{id}}'); });``` Is there any way to get the id as variable into the `eql(.)`? – Michael Dieblich Jan 30 '20 at 09:37
  • 1
    I wrote the correct expect statement for you, did you try that. That works, if you have `id` variable stored at the global level from a previous request. – Danny Dainton Jan 30 '20 at 10:42
  • what I didn't saw was the `pm.globals.get()`. My bad. You gave the right hint. The following code works: ```pm.test("IDs equal?", function () { var jsonData = pm.response.json(); pm.expect(jsonData.id).to.eql(pm.collectionVariables.get('id')); });``` – Michael Dieblich Jan 30 '20 at 11:07
  • So you didn't save this as a `global` variable, as you mentioned in the original question. :) – Danny Dainton Jan 30 '20 at 11:13