# How can I Validate Request Body Key and Value parameters that is in Form-data format

**URL:** <https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075>\
**Category:** Help Hub\
**Tags:** tests, automation, json\
**Created:** [March 1, 2022, 7:38pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075 "2022-03-01T19:38:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 1, 2022, 7:38pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/1 "2022-03-01T19:38:03Z")

</div>

How can I write a Test to validate Request Body Key and Value in Form-data format?

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 2:51pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/2 "2022-03-02T14:51:20Z")

</div>

How can I Validate Request Body Key and Value parameters that is in Form-data format  
pm.test("TESTNOW: Test Request Body Key and Value:- ", function () {

```
    pm.request.body.formdata.get("") // ENTER KEY HERE 

    pm.variables.get(""); // ENTER VALUE HERE

    console.log("TEST")        

    });

```

I tried the following but can’t get it to work

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 2, 2022, 2:55pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/3 "2022-03-02T14:55:14Z")

</div>

Hello @farhanhasanali, can you please help us with some more information?

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 3:32pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/4 "2022-03-02T15:32:27Z")

</div>

So we have an API that I am trying to write automation test to validate the Request Body that is in the form-data format with Key and Values… What I like to see if there is a way to capture the key and the value

1. Test- make sure the key and value is present and not null
2. Capture the key and value

the above script that i shared even if i put " " the test is passing my expectation was that if i dont put any value in the string than the test should have failed

I hope that helps

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 2, 2022, 3:40pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/5 "2022-03-02T15:40:00Z")

</div>

Hey @farhanhasanali, while I am trying to figure out a solution, have you gone through [this](https://community.postman.com/t/how-can-i-read-the-content-of-a-form-data-request-body-variable-in-the-test-script/15711) thread?

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 3:46pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/6 "2022-03-02T15:46:00Z")

</div>

Yes i have done that including checking stack flow threads

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 3:51pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/7 "2022-03-02T15:51:08Z")

</div>

Try to reformat Request data as JSON and then with ‘dots’ explore it

```auto
var response = pm.response.json();
var req = JSON.parse(request.data); //req because request is already taken as we extract data from it. 
//or 
var req = JSON.parse(request.body);

```

also tried this that did not work!

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 2, 2022, 3:58pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/8 "2022-03-02T15:58:28Z")

</div>

You can import this file : [https://www.getpostman.com/collections/ab68a847d37cdf288f8d](https://www.getpostman.com/collections/ab68a847d37cdf288f8d)

and below script where request data as JSON works with **dots**

```javascript
var data = pm.response.json()
var formDataKey = data.form.name
var formDataValue = data.form.message
console.log(formDataKey + "\n" + formDataValue)

```

Updating the post @farhanhasanali you can use `pm.expect` and other [test script examples](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/) to test the value.

```javascript
var data = pm.response.json()
var formDataKey = data.form.name
var formDataValue = data.form.message
console.log(formDataKey + "\n" + formDataValue)

pm.test("Request body to have form Data", function () {
    pm.expect(data.form.name).to.be.a("number").and.is.not.null.and.to.not.be.undefined.and.is.not.empty
    pm.expect(data.form.value).to.be.a("string").and.is.not.null
});

```

The working (I have converted the name to int later because accepts the value in text):

![working with form-data](https://us1.discourse-cdn.com/getpostman/original/3X/6/e/6e9910705bfcb10eec88efed2023a31f936162b5.gif)

I hope this helps 🪄

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 4:31pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/9 "2022-03-02T16:31:01Z")

</div>

let me try that thank you so much will update to see if that works

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 4:32pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/10 "2022-03-02T16:32:11Z")

</div>

Sorry i am new to automation and postman how do i import that file from the link you shared if you can guide me i would really appreciate it

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 2, 2022, 4:35pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/11 "2022-03-02T16:35:15Z")

</div>

Download the data from the link and save it as `somexamplename.json` and you can continue further steps [here](https://learning.postman.com/docs/getting-started/importing-and-exporting-data/#importing-data-into-postman)

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 2, 2022, 6:53pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/12 "2022-03-02T18:53:06Z")

</div>

I was able to get the file imported however the test case is failing but I am getting a 200 OK status and the values are coming across in the console with a warning sign?  
this is the script i used to update based on what I have in the form data value

Test Fail: Request body to have form Data | AssertionError: expected undefined to be a string

var data = pm.response.json()

```auto
    var formDataKey = data.form.grant_type

    var formDataValue = data.form.Scope

    console.log(formDataKey + "\n" + formDataValue)

    

    pm.test("Request body to have form Data", function () {

    pm.expect(data.form.grant_type).to.be.a("string").and.is.not.null.and.to.not.be.undefined.and.is.not.empty

    pm.expect(data.form.Scope).to.be.a("string").and.is.not.null

    console.log("Key string is " + data.form.grant_type + data.form.Scope)

    });

Console:
Request Body in console

body: “client_credentials”

Scope: “default”

Issue is since i am working with my company's Bearer Token when i try to run it on my collection from company i am getting the error 

There was an error in evaluating the test script: TypeError: Cannot read property 'grant_type' of undefined

If you can help me what i can do to bypass this so the test case can pass.
```

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 3, 2022, 7:35am UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/16 "2022-03-03T07:35:50Z")

</div>

Hey @farhanhasanali, looks like you are using wrong key, i.e., `grant_type` try using `data.form.body` instead of `data.form.grant_type`.

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 3, 2022, 2:21pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/17 "2022-03-03T14:21:26Z")

</div>

var data = pm.response.json()

```
    var formDataKey = data.form.body

    var formDataValue = data.form.body

    console.log(formDataKey + "\n" + formDataValue)

    pm.test("Request body to have form Data", function () {

    pm.expect(data.form.body).to.be.a("string").and.is.not.null.and.to.not.be.undefined.and.is.not.empty

    pm.expect(data.form.body).to.be.a("string").and.is.not.null

    });

```

There was an error in evaluating the test script: TypeError: Cannot read property ‘body’ of undefined

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 3, 2022, 2:23pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/18 "2022-03-03T14:23:00Z")

</div>

Can you share your response body that you send over the URL?

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 3, 2022, 2:42pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/20 "2022-03-03T14:42:49Z")

</div>

Oops! Not this. Can you `console.log(data)` on second line and share what’s the JSON data you are getting?

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 3, 2022, 2:49pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/23 "2022-03-03T14:49:06Z")

</div>

> [@farhanhasanali](#):
>
> var data = pm.response.json()

In your test script

```auto
var data = pm.response.json()
console.log(data)

```

check the console data here

 ![Screenshot 2022-03-03 at 8.18.24 PM](https://us1.discourse-cdn.com/getpostman/original/3X/e/2/e2822116251307b9d89e6a6ab8e9dca65dd04bc3.png)

---

<div class="post-metadata">

**Author:** ![sahuvikramp](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/sahuvikramp/32/10828_2.png) [@sahuvikramp](https://community.postman.com/u/sahuvikramp)\
**Post date:** [March 3, 2022, 3:58pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/26 "2022-03-03T15:58:16Z")

</div>

Try replacing the code with below and later add test using `pm.test` and `pm.expect` accordingly

```javascript
var data = pm.request.body
var myReq = data.formdata

var map = myReq.reduce((r, i) => ((r[i.key] = r[i.key] || []).push(i), r), {});
var mapValue = myReq.reduce((r, i) => ((r[i.value] = r[i.value] || []).push(i), r), {});

console.log(Object.keys(map));
console.log(Object.keys(mapValue));

```

collection link here : [Postman](https://www.postman.com/hellovikram/workspace/community-answers/request/10404977-97259cd4-5630-4df3-a89c-3526c4f1bfda)

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 3, 2022, 4:09pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/27 "2022-03-03T16:09:08Z")

</div>

let me try and update

---

<div class="post-metadata">

**Author:** ![farhanhasanali](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@farhanhasanali](https://community.postman.com/u/farhanhasanali)\
**Post date:** [March 3, 2022, 4:16pm UTC](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075/29 "2022-03-03T16:16:40Z")

</div>

No longer seeing error in the log but the test case is still failing!.. I am so humbled and I really appreciate all your help

[Next page](https://community.postman.com/t/how-can-i-validate-request-body-key-and-value-parameters-that-is-in-form-data-format/33075.md?page=2)
