0

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}}"
}
Sebi
  • 4,262
  • 13
  • 60
  • 116
  • 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 Oct 26 '20 at 20:47
  • 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 Oct 26 '20 at 20:50
  • 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 Oct 26 '20 at 20:52
  • 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 Oct 26 '20 at 20:53
  • Have you saved the request after making those changes? Can you provide some images showing what's happening. This is very one sided as no one can see what you can see in front of you – Danny Dainton Oct 26 '20 at 20:54
  • In the edit, you have a different variable name. – Danny Dainton Oct 26 '20 at 20:56
  • Ok. It works. Yes, the problem was that the variable had to be defined in the pre request script. – Sebi Oct 26 '20 at 20:57
  • @DannyDaiton Yep, other testcase :P – Sebi Oct 26 '20 at 20:58
  • Doing that with GET requests apparently doesn't work. Guess that user friendly =/= functional. I think that the requests execute correctly but it's just the URL that's not updated in the collection runner. – Sebi Oct 26 '20 at 21:12
  • Happy to walk through the issues you're seeing over on our Community forum, it would be easier to expand on things. https://community.postman.com – Danny Dainton Oct 26 '20 at 21:17
  • 1
    @DannyDainton https://community.postman.com/t/cannot-use-variables-in-get-requests/17110 – Sebi Oct 26 '20 at 21:29

1 Answers1

0

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.

Sebi
  • 4,262
  • 13
  • 60
  • 116
  • They can be assigned to run with individual requests, that's the normal usecase for them. The way that you were trying to run them didn't work because that's not the way you use Postman variables within the app. There are a number of different variables at different scopes, that work in different ways. – Danny Dainton Oct 26 '20 at 21:15