Search code examples
consoleenvironment-variablesglobal-variablespostmandata-files

Postman: data retrieved from an input file cannot be logged in console


I am using Postman for Windows Version 6.5.2.

Whenever I use an input file with variables, I would like to see variables I use in current test case printed out to console. For example I have a data file with list of user id:s. Then, at some point in my tests, I would like to send a simple message to console: "INFO: logging with user id XXXX."

I have tried assigning data to both environment and global variables. It seems not working neither in "Pre-request Script" section, neither in "Tests" section. In my case, if I have an initial value defined, then this value is being printed out all way around (despite that Postman takes different values from file for each iteration). If no value is defined (tested with both environment and global ones), then I get an empty string printed out.


Solution

  • Using the console.log(pm.iterationData.toObject()) statement in the Tests tab would log an object, containing the data from the file used in the request.

    My sample JSON data file:

    [
        {
            "item":"1",
            "item2": "Value 1"
        },
        {
            "item":"2",
            "item2": "Value 2"
        },
        {
            "item":"3",
            "item2": "Value 3"
        },
        {
            "item":"4",
            "item2": "Value 4"
        }
    ]
    

    This would log the following, when running the Collection from the Runner:

    Date File Log