# Test Script Error with sendRequest

**URL:** <https://community.postman.com/t/test-script-error-with-sendrequest/51097>\
**Category:** Help Hub\
**Tags:** tests, scripts\
**Created:** [August 29, 2023, 7:21am UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097 "2023-08-29T07:21:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thirumal33](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@thirumal33](https://community.postman.com/u/thirumal33)\
**Post date:** [August 29, 2023, 7:21am UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097/1 "2023-08-29T07:21:56Z")

</div>

I want to run the API until the expected result is achieved with some delay  
but getting this error

 ![Screenshot 2023-08-29 125039](https://us1.discourse-cdn.com/getpostman/original/3X/b/7/b7fd7f6d27db1ab1b635b5479cda6e608e760bb7.png)

```auto
var expectedStatus = "FINISHED";
var maxIterations = 10;

function runRequest(iteration) {
        var url = pm.environment.get("betaUrl");
        var token = pm.environment.get("token");
        console.log(token);
        var Url = "{{$url}}?authorization={{$token}}";
        console.log(`url: ${Url}`)
    pm.sendRequest(' https://mintapi.beta.thesilverlabs.com/graphql?authorization=23ec24bf1a722c0e92c049146fef63bd14ce1cccb25e37e9e8210a804a495c2b', function (err, response) {
        console.log(`response: ${JSON.stringify(response)}`)
        console.log(err)
        var responseBody = response.json();
        var currentStatus = responseBody.data.dominoTemplateVideoById.status;

        if (currentStatus === expectedStatus) {
            pm.visualizer.set(`
                <p>Iteration: ${iteration}</p>
                <p>Status: ${currentStatus}</p>
            `);
            return;
        }

        if (iteration === maxIterations) {
            pm.visualizer.set(`
                <p>Reached maximum iterations of ${maxIterations}</p>
                <p>Last checked status: ${currentStatus}</p>
            `);
            return;
        }

        pm.visualizer.set(`
            <p>Iteration: ${iteration}</p>
            <p>Status: ${currentStatus}</p>
            <p>Waiting for ${expectedStatus}...</p>
        `);

        setTimeout(function () {
            runRequest(iteration + 1);
        }, 2000);
    });
}

runRequest(1);

```

---

<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:** [August 29, 2023, 12:03pm UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097/2 "2023-08-29T12:03:11Z")

</div>

The error is telling you what is wrong.

I can’t vouch for the rest of the code, but the current problem is with the targeting of the status in this line.

```auto
var currentStatus = responseBody.data.dominoTemplateVideoById.status;

```

Can you post the response that is returned?

Console log the responseBody from the previous line. Expand the console log out so it shows all of the elements before creating the screenshot please and someone here should be able to help you with targeting the status correctly.

---

<div class="post-metadata">

**Author:** ![thirumal33](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@thirumal33](https://community.postman.com/u/thirumal33)\
**Post date:** [August 29, 2023, 1:29pm UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097/3 "2023-08-29T13:29:37Z")

</div>

![Screenshot 2023-08-29 185903](https://us1.discourse-cdn.com/getpostman/original/3X/c/6/c6b362f864f0e18238b52d9f912befce05052b27.png)

---

<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:** [August 29, 2023, 2:27pm UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097/4 "2023-08-29T14:27:28Z")

</div>

It would appear that your send request is failing.

Therefore its not responding with the body that you are expecting.

You will need to read the API specification to find out how to format the request correctly.

The error states “Must provide query string” as a starter for 10.

Get the sendRequest() working outside of the function first.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [November 27, 2023, 2:27pm UTC](https://community.postman.com/t/test-script-error-with-sendrequest/51097/5 "2023-11-27T14:27:42Z")

</div>

Closed on November 27, 2023, 2:27pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
