How to use extracted value (from previous response body) in next request's post body or in next request's header

How to extract a value from a response body and use it in next post body or in next call’s header. Here is the script snippet,

    "page_2 - https://login.np.v1/authorize",
    function () {
      response = http.get(
        "https://login.np.v1/authorize",
        {
          headers: {
            "upgrade-insecure-requests": "1",
            "sec-ch-ua":
              '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
          },
        }
      );
      const stateToken1 = findBetween(JSON.stringify(response), 'stateToken=', '\'');
      console.log(">>>>>>>>>>>",stateToken1)

      response = http.post(
        "https://login.np.mediaecosystem.io/api/v1/authn/introspect",
        '{"stateToken":"ObhVHBHbjhHBhBH_NH1562BHjhjhhjhywyHbhj"}',
        {
          headers: {
            accept: "application/json",
            "accept-language": "en",
            "content-type": "application/json",
            "x-okta-user-agent-extended": "okta-signin-widget-5.11.1",
            "sec-ch-ua":
              '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
          },
        }
      );

In the above code, I have fetched ‘stateToken’ value and save it in ‘stateToken1’. Now I want to use it in next subsequent requests. So what should I write in place of ‘ObhVHBHbjhHBhBH_NH1562BHjhjhhjhywyHbhj’ so that dynamic stateToken1 passes every time.

Welcome to the forum!

I’d suggest using JSON.stringify() to format your request body rather than just putting it in single quotes. Then you could just substitute your variable in place of the value for stateToken.

      response = http.post(
        "https://login.np.mediaecosystem.io/api/v1/authn/introspect",
        JSON.stringify({"stateToken":stateToken1}),
        {

If the variable were named stateToken instead of stateToken1, this could be further simplified to:

      response = http.post(
        "https://login.np.mediaecosystem.io/api/v1/authn/introspect",
        JSON.stringify({stateToken}),
        {

Alternately, you could use a Javascript template string surrounding the body with backticks instead of single quotes, and embed the variable using ${}:

      response = http.post(
        "https://login.np.mediaecosystem.io/api/v1/authn/introspect",
        `{"stateToken":"${stateToken1}"}`,
        {