How to get formdata feilds on loop?

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

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`);
        }
    }
}

Hey @maintenance-observe1 :wave:

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 :thinking: 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.

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

Consider the following…

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;
    });
});
1 Like

Yeah, I was a little confused about what it was actually doing :sweat_smile: - Seemed to be a lot of code for what it was trying to do.

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.

thanks, its really worth it for me…

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