# How to use response data from the the pre-request onto the same request

**URL:** <https://community.postman.com/t/how-to-use-response-data-from-the-the-pre-request-onto-the-same-request/40818>\
**Category:** Help Hub\
**Created:** [November 6, 2022, 4:03am UTC](https://community.postman.com/t/how-to-use-response-data-from-the-the-pre-request-onto-the-same-request/40818 "2022-11-06T04:03:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgeorge46](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mgeorge46/32/13864_2.png) [@mgeorge46](https://community.postman.com/u/mgeorge46)\
**Post date:** [November 6, 2022, 4:03am UTC](https://community.postman.com/t/how-to-use-response-data-from-the-the-pre-request-onto-the-same-request/40818/1 "2022-11-06T04:03:55Z")

</div>

I have PUT request {{url}}/api/v1/users/{{userid}} but the userid variable it’s value I need it to come from a pre-request script response data the request is a GET request {{url}}/api/v1/users/search?query=muddu1&limit=1

Kindly note this is under one PUT request of {{url}}/api/v1/users/{{userid}}

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [November 7, 2022, 3:12pm UTC](https://community.postman.com/t/how-to-use-response-data-from-the-the-pre-request-onto-the-same-request/40818/2 "2022-11-07T15:12:30Z")

</div>

Have you gone through the training yet as this covers basics like setting and retrieving variables and linking requests together.

[https://www.postman.com/postman/workspace/postman-galaxy-training/overview](https://www.postman.com/postman/workspace/postman-galaxy-training/overview)

Run through the “APIs 101 Training”, then the “Testing and Automation Training” course.

If you must have it as a pre-request script, then the GET request has to use sendRequest and you’ll need to get the element from the request and store it in a variable. (I would recommend environment or collection scope).

However, does it really need to be a pre-request script?  
You could have a folder, with the GET request first, and the PUT request following.  
You can then save the userid from the GET request response to a variable through the tests tab of that request which can be used in the following PUT request.

> **[Using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/)**
>
> Using variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![mgeorge46](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/mgeorge46/32/13864_2.png) [@mgeorge46](https://community.postman.com/u/mgeorge46)\
**Post date:** [November 8, 2022, 4:39pm UTC](https://community.postman.com/t/how-to-use-response-data-from-the-the-pre-request-onto-the-same-request/40818/3 "2022-11-08T16:39:22Z")

</div>

Hello @michaelderekjones,

Thanks for your help I finally got it working and the “APIs 101 Training” Helped however following your advice of using the GET request first then I use the PUT request in the TEST tab is also great but here is the simple Pre-request script that got it working as per my first request.

```auto
const getRequest = {
  url: 'https://{{url}}/api/v1/users/search?query={{userid}}&limit=1/get',
  method: 'GET',
  header: {
    'Content-Type': 'application/json',
    'X-Foo': 'bar',
    'Authorization': 'Basic xxxxxyyyyzzzzxxxxyy',
  }
};
pm.sendRequest(getRequest, (error, response) => {
    pm.expect(response).to.have.property('code',200)
    let responseJson=response.json();
    pm.variables.set("userid",responseJson[0].id);
    //console.log(responseJson[0].id)
});

```
