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

14,054

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

Share:
14,054
Lalit
Author by

Lalit

Updated on June 05, 2022

Comments

  • Lalit
    Lalit almost 2 years

    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.

    • Danny Dainton
      Danny Dainton about 6 years
      Have you actually tried reading the Postman documentation or their blog? blog.getpostman.com/2014/10/28/…
    • Lalit
      Lalit about 6 years
      Yes I have gone through the article and followed the steps. The script fails and says " "A non-empty request body is required.". But with " Here is the screen short
    • Lalit
      Lalit about 6 years
      With manual run and with mapping in csv file from There was an error running your collection: Invalid URL "xxxxxxxx". Where as works fine with mentioned raw JSON format
    • Danny Dainton
      Danny Dainton about 6 years
      Update your question with all the details about what you have and haven't done so far. Also add any scripts that you're using. This is be better than trying to half explain in the comments.
    • Lalit
      Lalit about 6 years
      In body, I am passing raw data as (application/json) "rateCalculationFilters": [ { "fieldName": "EquipmentModel", "fieldValue": "0" Ex different set of data:2009,1992 }, {"fieldName": "TERM", "fieldValue": "3" Ex:2,7 }, {"fieldName": "POWERUPRATE", "fieldValue": "75000" 4000, 90000 } ] Wrote 11 calls for 11 set of data. when data will increase, have to write more calls & maintenance will be more Expectation: I want to pass data from csv file & run same script for number times with different set of data, rather than individual script
    • Danny Dainton
      Danny Dainton about 6 years
      "Update your question with all the details"
    • Lalit
      Lalit about 6 years
      Updated the question, please take a look
  • Lalit
    Lalit about 6 years
    Thank you, applied the logic you provided and it is working fine with CSV file now. But tried with same in JSON, but it run only one time, not picking the same set of data.
  • Danny Dainton
    Danny Dainton about 6 years
    How many iterations did you set? The default is 1 so if you just pressed the button it would only pick up the first set of data.
  • Lalit
    Lalit about 6 years
    Thanks Danny, you are right. With JSON we have to give no of iteration . Where as with CSV iteration picks automatically, number of records csv file has
  • Lalit
    Lalit about 6 years
    Facing similar issue, while verify the result with variables Here is verification script pm.test("Matching interest rate 4.29", function () { pm.expect(pm.response.text()).to.include("4.29"); }); Now I want to check the result in "Test", same way as passing variable. Here is the code I tried pm.test("Matching interest rate 4.29", function () { pm.expect(pm.response.text()).to.include(data["ExpectedResul‌​t"]); }); When I see PREVIEW DATA in collection runner, it shows 4.29. It fetches the data, but result shows failed. Can you help me out to solve this issue.
  • Danny Dainton
    Danny Dainton about 6 years
    This is another question. Try not to ask multiple question in the same question - Create another one with the new details.
  • Lalit
    Lalit about 6 years
    Create another question. Here is the link stackoverflow.com/questions/49277397/…
  • Rocky4Ever
    Rocky4Ever almost 4 years
    Tried {{}} but i see my collection is not taking from file.Still has {{lastName}} itself not replaced with values from file.