# How to make api request inside of test

**URL:** <https://community.postman.com/t/how-to-make-api-request-inside-of-test/10903>\
**Category:** Help Hub\
**Created:** [February 20, 2020, 3:31am UTC](https://community.postman.com/t/how-to-make-api-request-inside-of-test/10903 "2020-02-20T03:31:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derrick.Boyd](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@Derrick.Boyd](https://community.postman.com/u/Derrick.Boyd)\
**Post date:** [February 20, 2020, 3:31am UTC](https://community.postman.com/t/how-to-make-api-request-inside-of-test/10903/1 "2020-02-20T03:31:49Z")

</div>

Inside of an api test, i would like to get data from another api to compare against. ex;

Send GET request to [abc.com/user](http://abc.com/user)

- In the test section -

jsonData = pm.response.json()  
pm.globals.set(“user”, jsonData.user)

pm.test(“User data is valid”, function () {

const getData ={  
url: [www.123.com/user](http://www.123.com/user),  
method: ‘GET’,  
header: {{ header}}

};

pm.sendRequest(getData);  
jsonRes = pm.response.json()  
pm.globals.set(“user2”, jsonRes.user)

pm.expect(user2).to.equal(user);

});

Even though i get a 200 on the call, the response, when logged, is not properly stored. Am i doing something wrong here?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [February 20, 2020, 5:25am UTC](https://community.postman.com/t/how-to-make-api-request-inside-of-test/10903/2 "2020-02-20T05:25:00Z")

</div>

What you’re doing wrong is that `pm.sendRequest` takes two parameters, the first is the request options and the second is the callback.

When you’re writing `pm.response.json()` it is referring the actual request that was being sent by postman, not the one that you explicitly defined in the test scripts.

What you need to do is this:

```javascript
pm.sendRequest(getData, (err, response) => {
   let body = typeof response === 'string' ? JSON.parse(response) : response;
   pm.globals.set('user2', body.user);

  //and so on...
});
```
