Using a POST request with body data imported from a CSV file in Postman

Lalit picture Lalit · Mar 7, 2018 · Viewed 12.4k times · Source

I am new to POSTMAN and facing an issue with driving data from an external source like a CSV file and using this to pass in data to the request body, rather than writing the same script multiple times with different sets of data.

In the request body, I am passing in raw data as (application/json)

{
    "groupCode": "FAFCE",
    "associationCode": "",
    "programTypeCode": "NE",
    "rateCalculationFilters": [
        {
            "fieldName": "EquipmentModel",
            "fieldValue": "0" //<Ex different set of data:2009,1992 >
        }, 
        {
            "fieldName": "TERM",
            "fieldValue": "3" //<Ex 3,7,4 >
        }, 
        {
            "fieldName": "POWERUPRATE",
            "fieldValue": "75000" //<Ex set of data:82009,77992 
        }
    ]
}

I wrote 11 calls for 11 sets of data. When the data will increase, I have to write more calls & maintenance will be more than my Expectation. I want to pass data from a CSV file and run same script for number times, with different sets of data, rather than an individual script.

Answer

Danny Dainton picture Danny Dainton · Mar 9, 2018

You could achieve this by using the {{...}} syntax in the POST request body like this:

{
    "groupCode": "FAFCE",
    "associationCode": "",
    "programTypeCode": "NE",
    "rateCalculationFilters": [
        {
            "fieldName": "EquipmentModel",
            "fieldValue": {{EquipmentModel}}
        },
        {
            "fieldName": "TERM",
            "fieldValue": {{TERM}}
        },
        {
            "fieldName": "POWERUPRATE",
            "fieldValue": {{POWERUPRATE}}
        }
    ]
}

And then creating a CSV or a JSON file to populate these placeholders during the 11 requests.

This is an example using a JSON data file, on each iteration run it will use each set of values from the file:

[
    {
        "EquipmentModel": 1,
        "TERM": 1,
        "POWERUPRATE": 1
    },
    {
        "EquipmentModel": 2,
        "TERM": 2,
        "POWERUPRATE": 2
    }
]

Postman