From 796a3dd2a0665ff69a0cda98b457d55725efdf11 Mon Sep 17 00:00:00 2001 From: Tshepo Mgaga Date: Sun, 28 Mar 2021 06:56:23 +0200 Subject: [PATCH 1/2] Update cheatsheet.rst Added an assertion example to check if a key exists in a json response. --- docs/cheatsheet.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/cheatsheet.rst b/docs/cheatsheet.rst index b036113..a68934b 100644 --- a/docs/cheatsheet.rst +++ b/docs/cheatsheet.rst @@ -294,6 +294,15 @@ Nested value check: :: pm.expect(response.products.0.category).to.eql('Detergent'); +Check if a key exists in the response: :: + + pm.test("Should have an auth token", function () { + var jsonData = pm.response.json(); + pm.expect(_.has(jsonData, 'data.token')).to.be.true; + }); + +Note: lodash is built into the sandbox. More info https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries + **XML responses** Convert XML body to JSON: :: From ac9c599fd7a9795a937a6350dc657e65e530bfcd Mon Sep 17 00:00:00 2001 From: Tshepo Mgaga Date: Sun, 28 Mar 2021 07:33:58 +0200 Subject: [PATCH 2/2] Check if property exists in JSON response Simple example to check if a property exists in the JSON response --- docs/cheatsheet.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/cheatsheet.rst b/docs/cheatsheet.rst index a68934b..90e47dd 100644 --- a/docs/cheatsheet.rst +++ b/docs/cheatsheet.rst @@ -298,11 +298,9 @@ Check if a key exists in the response: :: pm.test("Should have an auth token", function () { var jsonData = pm.response.json(); - pm.expect(_.has(jsonData, 'data.token')).to.be.true; + pm.expect(jsonData).to.have.nested.property('data.token'); }); -Note: lodash is built into the sandbox. More info https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries - **XML responses** Convert XML body to JSON: ::