Hello everybody,
I am using the Postman Runner and it works pretty smooth when i want to make some calls (patch, post, put) for the same guid in a particular order. But now i would like to make several get calls and use the results in the body of a post call. For example
GET call 1
âstartDateâ: â2020-01-01â
âemployeeProfileIdâ: {
âkeyâ: 10,
âvalueâ: âEmployee Aâ
},
GET call 2
âfunctionâ: {
âkeyâ: â2a18618f-81e3-4259-82ca-66cf79ea33d8â,
âdescriptionâ: âCommonâ,
âgroupâ: null
},
POST call
âstartDateâ: â2020-01-01â
âemployeeProfileIdâ: {
âkeyâ: 10,
âvalueâ: âEmployee Aâ
},
âfunctionâ: {
âkeyâ: â2a18618f-81e3-4259-82ca-66cf79ea33d8â,
âdescriptionâ: âCommonâ,
âgroupâ: null
},
Is this possible and if so, how do i do this? I found some articles that hint on using Tests an Pre-request scripts but not with a clear explanation
for a case like this. I did manage to use Tests to fetch data and transfer it to the variables but no easy way to fetch data and transfer it to the body of a Post all.
Hoping for some good advice, very much appreciated.
Rob
If you are new to Postman I would recommend the Postman training links which are located under âother resourcesâ in the Learning Centre.
Other resources | Postman Learning Center
The âGalaxy APIâs 101â course gets you used to sending requests and the GUI.
The âGalaxy Testing and Automationâ gets you used to testing your responses and using variables, basic scripting, etc.
This should cover your scenario.
Also take a look at the following.
Postman JavaScript reference | using-environment-variables-in-scripts
Finally, Postman uses JavaScript under the hood, If you are going to be scripting, I would also recommend learning some JavaScript basics. WC3 schools is a good place to start.
Go through the training courses, and if you hit any issues. Post what youâve got and someone here should then be able to help with your specific request.
Please use the preformatted text option in the editor when pasting code. Itâs stops everything from being aligned to the left and keeps the indents and formatting.
Thank you:)!
The articles have been very helpfull and i am now able to automatically use the data of a get in the body of a post. However i am receiving a âCannot read properties of undefined (reading â0â)â error in case there is no value chosen from the underlying table, so when its null.
The below input in Tests works fine if the GET can return data from the underlying table. But if no choice is made from the underlying table, so when its null, i get the error message.
bodydata = JSON.parse (responseBody) // to parse json response
valuestandardFunction = bodydata._embedded[0].standardFunction.key
console.log (âvalue of standardFunction:â + valuestandardFunction)
pm.environment.set(âstandaardfunctieâ, valuestandardFunction);
Somehow i want it to make the choice to get the info if there is any and otherwise use null. I tried with âto.be.oneOfâ but somewhere i am doing something wrong. I hope someone can give me some insight in this.
Thanks!
JSON\JavaScript objects are in key /value pairs.
Values can be a string, null, an array, or another object. (It can get complex).
undefined is not the same as null.
undefined means it canât find the key.
null means it can find the key, but its null.
Although you can probably deal with undefined elements, I recommend that you donât.
You need to be in control of your test data. It needs to return the same result each and every time and you base your tests on those results.
If you want to test null, then you ensure that you have test data that will illicit that response. (Which means you might have more than one request to the same end point to test out all of these scenarios).
Thanks for the answer. I havenât been able to fix it yet but i am getting closer. Iâll formulate my question differently in a separate post.