# Problem with x-www-form-urlencode + .csv + tests

**URL:** <https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938>\
**Category:** Help Hub\
**Tags:** tests, csv\
**Created:** [November 23, 2023, 3:43pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938 "2023-11-23T15:43:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![klestok](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@klestok](https://community.postman.com/u/klestok)\
**Post date:** [November 23, 2023, 3:43pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/1 "2023-11-23T15:43:51Z")

</div>

hi there!  
I have 12 post requests. The content type is "application/x -www-form -urlencoded. each one contains one test. When simply running the collection, everything works and the tests pass. but when using the csv file, for some reason, 144 tests are performed with 12 iterations. 15 of them pass. all the variables are global.  
this is the example of reqest body -

 ![Screenshot_11](https://us1.discourse-cdn.com/getpostman/original/3X/f/3/f3ffc7b8d1570415d758c2fc4baea575d835f994.png)  
and this is my csv file (in readable form) -  
 ![Screenshot_1](https://us1.discourse-cdn.com/getpostman/original/3X/e/a/eae25a1ecb88d6896e043a7e48f5e883335ccfc4.png)  
please tell me in which direction to look for the error? Why am I getting 144 tests?

---

<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 23, 2023, 3:52pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/2 "2023-11-23T15:52:34Z")

</div>

What do you think should happen?

When you use an data file, it will run each of the 12 requests once for each line in the CSV file. Each line in the CSV file is an iteration.

So if you have 12 lines in your CSV file with 12 requests, then its going to be 144 requests. Therefore if you have 1 test in each of those requests, it will also be 144 tests.

You haven’t mentioned what the test is?

Why global variables, do you really need these values available to all of your collections in Postman?

---

<div class="post-metadata">

**Author:** ![klestok](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@klestok](https://community.postman.com/u/klestok)\
**Post date:** [November 23, 2023, 4:19pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/3 "2023-11-23T16:19:18Z")

</div>

thank you for your answer. by test I mean just test -

 ![Screenshot_2](https://us1.discourse-cdn.com/getpostman/original/3X/5/e/5e4f597b7431a4986dcf96329bd20839ea8d8bc8.png)  
one line of file = one request = one test. i think so.  
if i run collection without tests, i have 12 iterations, 12 reqests, that’s why i expect to have 12 test results

---

<div class="post-metadata">

**Author:** ![klestok](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@klestok](https://community.postman.com/u/klestok)\
**Post date:** [November 23, 2023, 4:49pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/4 "2023-11-23T16:49:13Z")

</div>

I understood what you meant… only now I don’t understand how to run a collection of these 12 requests so that each request corresponds to only one line of the data file

---

<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 23, 2023, 5:56pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/5 "2023-11-23T17:56:20Z")

</div>

Do you really need 12 requests, or just two, or potentially one.

- Sale Request with Account Number
- Sale Request with Track Data

Are all the requests against the same end point with different data?

So it could just be…

- Sale Request

The aim is to loop though your CSV but you sort of need to know which data points have to change within each request and which might be static through all requests to work out the best way to split things up, and whether it can be done with one request or really does need to be split up into 12 requests.

I’m wondering if it would be possible to have a single request single request with all of the fields that are static, and then use a pre-request script to retrieve the fields from the CSV and then add these to the request.

All of the data for each iteration is held within a special “data” dictionary.

If you just console log the data variable in your pre-request script, you should see an JavaScript object with all of the elements for that iteration.

```auto
console.log(data);

```

To update the body for an urlencoded request, it would be something like the following.

Considering the following basic request to Postman Echo.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/5/65c4e9d90d212402a6714f1bf61733c7d28128af.png)

In the pre-request script, you can add keys and values to the request.

```auto
let elementstoAdd = {"key":"password", "value":"123456"}
pm.request.body.urlencoded.add(elementstoAdd);

```

Which you can see were added into the request.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/b/4b462604bc438c0cfaee2e962857119ff89843ee.png)

To loop though the data variable (which should be a single line of your CSV) and update the body, you can do the following…

```auto
for (let [key, value] of Object.entries(data)) {
    console.log(key, value);
    if (value) {
        pm.request.body.urlencoded.add({"key": key, "value": value});
    }
}

```

It will check if a particular column has a value, as I’m assuming one of the issues is that some of the lines in the CSV have data in certain columns and others don’t.

If you were sending the same elements on every request, then you probably don’t need any of this and should be able to just reference them directly as variables using {{data.columnname}} in your body.

The above is only really needed if certain requests have more or less elements that you have to cater for.

---

<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:** [December 23, 2023, 5:57pm UTC](https://community.postman.com/t/problem-with-x-www-form-urlencode-csv-tests/54938/6 "2023-12-23T17:57:01Z")

</div>

Closed on December 23, 2023, 5:57pm UTC

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