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

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

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

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

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

Hey @farhanhasanali, while I am trying to figure out a solution, have you gone through this thread?

Yes i have done that including checking stack flow threads

1 Like

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

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!

You can import this file : https://www.getpostman.com/collections/ab68a847d37cdf288f8d

and below script where request data as JSON works with dots

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 to test the value.

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

I hope this helps :magic_wand:

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

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

Download the data from the link and save it as somexamplename.json and you can continue further steps here

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()

    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.

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

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

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

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

In your test script

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

check the console data here

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

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

let me try and update

1 Like

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