0
votes

This sounds basic but I have no clue what postman is doing; my setup is as follows:

input file:

[
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "8f338a25-c52a-4c47-9d5f-69a6c48c24f2",
    "user_id": ""
},
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "32461687-530e-4730-86c4-00fa4c284cd9",
    "user_id": ""
},
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "913ab956-b89d-41a2-9c09-0a970e202440",
    "user_id": ""
},
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "55c30784-3ad1-4a4e-a781-51a6f0f2fa42",
    "user_id": ""
},
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "55c30784-3ad1-4a4e-a781-51a6f0f2fa42",
    "user_id": ""
},
{
    "url": "http://localhost:3000/gadgets/",
    "gadget_id": "",
    "user_id": "4b1c6bc3-d44d-492b-8360-d5076913578b"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "db0722e7-3224-4db1-8a94-372ab462ae70"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "8d0f87f1-6697-4e08-8ddd-0e34e2015787"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "9d788283-87a9-4c38-8d48-30ddc1604058"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "8a86b353-df4c-40fd-a5a1-cfa5f27c41c4"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "a67485e5-fb13-4863-ab55-01fd58c2600f"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "3ab7211d-99e7-4cc6-a862-ac106c2a24de"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "378b2e77-7e18-4d75-b780-c9281b86c8ee"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "df1e35ca-37c5-4026-aa59-ebc02e348fdf"
},
{
    "url": "http://localhost:3000/users/",
    "gadget_id": "",
    "user_id": "2e0aadab-efe2-42e5-9088-c0bb1c6c9631"
}
]

and a POST request in order to create gadgets:

body (raw/JSON):

{
    "id": {{gadget_id}}
}

Pre-request script:

// Initialize ids
var gadget_id= pm.iterationData.get("gadget_id");

According to:

https://blog.postman.com/using-variables-inside-postman-and-collection-runner/

this should be enough. The body is not created (id is set empty).

How can I make postman read the file and assign a value to the variable correctly?

Edit:

Tried wrapping the variable into double quotes but it's still not set:

{
    "id": "{{gadget_id}}"
}
1
Couple of things - you don't need to add that to the pre-request if that's the only variable with that name in the collection. Ensure that all the requests in the collection are saved before running the collection in the Runner. You might need to wrap that variable in the body with quotes.Danny Dainton
The variable in this instance is intended to be an array of ids read from a JSON file; I want to have a separate POST request for each separate id that's stored in that variable. I find countless examples of setting a singular variable but none where the value can be iterated through.Sebi
Putting double quotes and clearing the pre-request script does not fix the issue; the body still doesn't have an id assigned to it.Sebi
OK, the datafile is a string for that variable so that's why I suggested that in the body. Your question and your usecase are different - might be a good idea to edit the question and clarify what you're trying to do.Danny Dainton

1 Answers

0
votes

It didn't seem intuitive at first; but postman assigns variable values only while running the Collection Runner. Variables cannot be assigned when running individual requests.

Setup:

prerequest script:

var gadget_id = pm.iterationData.get("gadget_id");

body:

{
    "id": "{{gadget_id}}"
}

All requests are run in parallel in the collection runner; I have no idea how to run sets and subsets of requests in a given sequence.