-1

Hello I am now to javascript and writing script for API testing in Postman to automate my usecases. Need help for below query.

Lets say i have my API request as below.

API request: https://reqres.in/api/users/2

Response:

{
   "data":{
      "id":2,
      "email":"janet.weaver@reqres.in",
      "first_name":"Janet",
      "last_name":"Weaver",
      "avatar":"https://reqres.in/img/faces/2-image.jpg"
   },
   "support":{
      "url":"https://reqres.in/#support-heading",
      "text":"To keep ReqRes free, contributions towards server costs are appreciated!"
   }
}

Query is from the response if the data.id == 2 then i have to read the 'email' & 'avatar' keys from global variables of postman and start the validation.

Code:

const response = JSON.parse(responseBody);
if (response.data.id === 2) {
//Here i have to read the 'email' & 'avatar' keys from global variables of postman.
tried response.data.pm.globals.get('email') & response.data.pm.globals.get('avatar') but its failing as the calling itself is not proper i belive.
}

Please help and thanks in advance.

Fido_de07
  • 73
  • 1
  • 6
Driod
  • 23
  • 6
  • The question is confusing, so you want to read the values from JSON, right? Why not just: `var name = response.data.first_name; var mail = response.data.email;` – Fido_de07 Dec 25 '22 at 15:41
  • Yes i can read like how you mentioned, but my json will change dynamically, So first i will check the id based on that will read the email and avatar from my global variables. Hope this is clear – Driod Dec 25 '22 at 15:48
  • What lesson or tutorial is telling you to use this? `JSON.parse(responseBody)`? Just use `pm.response.json()` – Danny Dainton Dec 25 '22 at 19:09
  • @DannyDainton Problem is not about reading the response json body. Here, let's say in my response if the response.data.id equals 2 then I need to print the values of my global variables(email & avatar) by appending them to the response.data.pm.globals.get('email') & response.data.pm.globals.get('avatar'). i know that i can directly call response.data.email & response.data.avatar to get the values, but here my question was is there a way to append the global variables? – Driod Dec 26 '22 at 03:17
  • I'm aware of what the problem is, I just get annoyed when I see that as it just looks like it's been copy and pasted from somewhere without any actual thought. – Danny Dainton Dec 26 '22 at 07:19
  • The question does not make sense. Why are you wanting to do that? You can either read the values from the response (`console.log(response.data.email)`) or print out the variable (`console.log(pm.globals.get('email')`) I don't know what you're talking about by appending. What's an example of the end result? – Danny Dainton Dec 26 '22 at 07:25

1 Answers1

0

Use this code in Tests section

const response = JSON.parse(responseBody);

if (response.data.id === 2) {
    pm.globals.set("email", response.data.email);
    pm.globals.set("avatar", response.data.avatar);
    console.log(pm.globals.get("email"))
    console.log(pm.globals.get("avatar"))
}

You can see it's result at console.

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14