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.