1
votes

My sample JSON file for postman runner:

[ { "name": "runner", "hitler_id": "4006abc", "year": "2017", "boolean": "false", "expected": 717962 } ]

Pre request script:

var member = data.name; var booking = data.boolean; var fyyear = data.year; var sid = data.hitler_id;
console.log(data.name); console.log(data.boolean); console.log(data.year); console.log(data.hitler_id);

Body with parameters:

{ "size": 0, "query": { "bool": { "filter": [ { "terms": { "name": [ "{{name}}" ] } }, { "terms": { "salesman_id": [ "{{sid}}" ] } }, { "terms": { "fyyear": [ "{{fyyear}}" ] } }, { "terms": { "boolean": [ "{{boolean}}" ] } } ] } }, "aggs": { "year": { "terms": { "field": "year" }, "aggs": { "value": { "sum": { "field": "value" } } } } } }

For only string variables are accepted - name and boolean fields are working and the value is populated for the other two, the variable values are not passed.

1

1 Answers

0
votes

The variables are not used in your request body that way. Either you have to store them in environment oder global variables via

pm.globals.set("variable_key", variable_value)
pm.environment.set("variable_key", "variable_value");

or just skip the pre-request script if you just want to use your data and reference the fields directly in your body:

{
    "size": 0,
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "name": [
                            "{{name}}"
                        ]
                    }
                },
                {
                    "terms": {
                        "salesman_id": [
                            "{{hitler_id}}"
                        ]
                    }
                },
                {
                    "terms": {
                        "fyyear": [
                            {{year}}
                        ]
                    }
                },
                {
                    "terms": {
                        "boolean": [
                            {{boolean}}
                        ]
                    }
                }
            ]
        }
    },
    "aggs": {
        "year": {
            "terms": {
                "field": "year"
            },
            "aggs": {
                "value": {
                    "sum": {
                        "field": "value"
                    }
                }
            }
        }
    }
}

However take care you're storing the values in your data file. You stored the bool and the year as strings". But they should be represented as you already did for the "expected" var.