# How to get formdata feilds on loop?

**URL:** <https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328>\
**Category:** Help Hub\
**Tags:** tests, variables, collections, api\
**Created:** [December 5, 2023, 1:45pm UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328 "2023-12-05T13:45:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![maintenance-observe1](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/maintenance-observe1/32/26714_2.png) [@maintenance-observe1](https://community.postman.com/u/maintenance-observe1)\
**Post date:** [December 5, 2023, 1:45pm UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/1 "2023-12-05T13:45:39Z")

</div>

i try to get formData for validate my data for that should be string but its not getting formdata :

```auto
let formData = pm.request.body.formdata;
let checkString = ["email", "password"];

for (var i = 0; i < formData.length; i++) {
    var currentField = formData[i];
    console.log(currentField)

    for (var j = 0; j < checkString.length; j++) {
        var allowedProperty = checkString[j];
        console.log(allowedProperty);
        
        // Ensure that the property is not undefined
        pm.expect(currentField.value).to.not.be.undefined, `${allowedProperty} should not be undefined in field ${i + 1}`;
        
        // Ensure that the property is a number (data type check)
        pm.expect(currentField.value).to.be.a('number', `${allowedProperty} should be a number in field ${i + 1}`);

        // Log the value of the property
        console.log(`${allowedProperty} in field ${i + 1}: ${currentField.value}`);

        // Check if the property has an empty value
        if (currentField.value === "") {
            pm.expect.fail(`${allowedProperty} in field ${i + 1} has an empty value`);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 5, 2023, 4:50pm UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/2 "2023-12-05T16:50:43Z")

</div>

Hey @maintenance-observe1 👋

Welcome to the Postman Community! :postman:

I can see that you’re using `pm.expect()` but I don’t see these within a `pm.test()` function 🤔 That wouldn’t show anything in the Test results section without that.

This should be `let formData = pm.request.body.formdata.all();` I believe, to access that array.

---

<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:** [December 5, 2023, 5:31pm UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/3 "2023-12-05T17:31:30Z")

</div>

It doesn’t look like you are actually checking that the allowed properties exist?

Consider the following…

```auto
let formData = pm.request.body.formdata.all();
// console.log(formData);

let allowedProperties = ["email", "password"];

formData.forEach(field => {
    pm.test(`Form Data Check ${field.key}`, function () {
        // console.log(field.value);
        pm.expect(field.key).to.be.oneOf(allowedProperties);
        pm.expect(field.value).to.not.be.undefined;
        pm.expect(field.value).to.not.be.empty;
    });
});

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [December 5, 2023, 5:34pm UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/4 "2023-12-05T17:34:54Z")

</div>

Yeah, I was a little confused about _what_ it was actually doing 😅 - Seemed to be a lot of code for what it was _trying_ to do.

---

<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:** [December 6, 2023, 10:08am UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/5 "2023-12-06T10:08:26Z")

</div>

If we’re discussing the actual test, I don’t understand it at all as its just checking what you’ve set in the request which surely you should know.

I’m not quite seeing the value of the test.

Yes, you can run tests in the pre-request scripts, and yes you can check the request details, but usually tests are against the responses. There is an assumption that you know what information you are sending.

---

<div class="post-metadata">

**Author:** ![maintenance-observe1](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/maintenance-observe1/32/26714_2.png) [@maintenance-observe1](https://community.postman.com/u/maintenance-observe1)\
**Post date:** [December 6, 2023, 11:13am UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/6 "2023-12-06T11:13:20Z")

</div>

thanks, its really worth it for me…

---

<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:** [January 5, 2024, 11:13am UTC](https://community.postman.com/t/how-to-get-formdata-feilds-on-loop/55328/7 "2024-01-05T11:13:53Z")

</div>

Closed on January 5, 2024, 11:13am UTC

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