Postman: Define a variable from list - postman

I'm using Postman and wondering if I can use a stored JSON object to create variable for additional calls. For an example: I saved an array which include name and ID:
[{"id":28,"name":"Action"},{"id":12,"name":"Adventure"},
{"id":16,"name":"Animation"},{"id":35,"name":"Comedy"},
{"id":80,"name":"Crime"},{"id":99,"name":"Documentary"},
{"id":18,"name":"Drama"},{"id":10751,"name":"Family"},
{"id":14,"name":"Fantasy"},{"id":36,"name":"History"},
{"id":27,"name":"Horror"},{"id":10402,"name":"Music"},
{"id":9648,"name":"Mystery"},{"id":10749,"name":"Romance"},
{"id":878,"name":"Science Fiction"},{"id":10770,"name":"TV Movie"},
{"id":53,"name":"Thriller"},{"id":10752,"name":"War"},
{"id":37,"name":"Western"}]
I'm triggering another API (second call) that retrieves only IDs, so the response is like this: "genre_ids": [35, 10402]
Is there a way to create an environment variable that looks for the IDs, fetch the relevant name from the second API and create a name oriented variable so on the case above 35=comedy and 10402=music so the variable will be: comedy,music?

to save environment variable you can do the following (see the snippets on the right side of postman):
postman.setEnvironmentVariable("variable_key", "variable_value");
if you want to save a global variable just do :
postman.setGlobalVariable("variable_key", "variable_value");
and then use them as you want.
Alexandre

Definitely it's possible with "Tests"(or Post-Request) script.
But since you have 2 requests(and Postman force you there should be 2 separated requests) you should save response from first request into variable with setEnvironmentVariable or setGlobalVariable and after getting response for 2nd request - to parse response for first one and iterate over it, looking up by id given.

Related

Postman, Set on evraibale for "run session"

I know that we can set a variable in different scopes via a pre-request script, but can we set one for on "execution" or "run of test".
I have a folder that contains two requests to validate a scenario where the first one will create a resource with an unique id and the second one will fail by trying to create a resource with the same unique id.
I would like to generate that unique value each time the collection is run. At this time I use a collectionVariables to test and set when not present but that variable is kept between each "retry".
Can I create a variable that will be the same only for one execution of a collection ?
Thanks
I have similar cases, where I store the values in Environment variables and then unset them in the Pre-request script of the first request:
pm.environment.unset("myVariable");
So, my solution is the same as the one suggested by #so cal cheesehead.
I create a variable in either the folder pre-request or the first request script. And unset it after the last test in the last request.
The sad part is that the initialization and destruction of this variable is spread in different scripts.

How can I save value of dynamic variable in Postman?

Postman allows to generate random dummy data by using pre-defined variables, on example this one would be replaced by random company name:
{{$randomCompanyName}}
Using pre-defined variables multiple times return different values per request.
The question is how to save once generated value to the variable for further usage on example in tests, something like(it doesn't work):
pm.variables.set("company", {{$randomCompanyName}});
Thanks.
You can use the .replaceIn() function with that {{...}} syntax in the sandbox.
pm.globals.set("company", pm.variables.replaceIn('{{$randomCompanyName}}'));
I've used a global variable to store the value as you would want to use it again. You could also use either the environment or collectionVariables scope to do the same thing.

How to interpolate environmental variable as number in json body of request in Postman?

My API consumes data in JSON where some fields need to be numbers. I have some shared data in my environmental variables, where one entry is user_id, which in my case is an integer, so I need to provide it to my API as a number.
I've tried to inline value "userId": {{user_id}}, but it highlights coma after last } and API can't see what is coming to it. In Postman console I can see that actual value sent is this:
"user": {{user_id}},
So it doesn't seem to work. And using "userId": "{{user_id}}" doesn't work in my case as user id will be sent as a string.
How to interpolate environmental variable as number in json body of request?
I've mistyped the name of an environmental variable. I was using {{user_id}}, instead of {{userID}} that is in my environment, thus interpolation resulted in the different wrong thing.

How to automatically get or create a doc with a field value in couchdb in a single request?

In a certain scenario, I want to pass a field value(in string format) to the CouchDB and get associated doc (or only its id) which contains that particular string value in one its fields. In case, if no doc contains that particular field value, I would like CouchDB design functions to automatically create one and return the newly created doc.
I can accomplish this by making a GET request followed by a PUT request if there is no doc with that particular field value. Is there any way to get this done with just one POST request?
Design document functions (other than updates) cannot modify the data in any way.
So no, this is not possible.
You can write a list function to return you a new document if the results are empty, but it cannot save it automatically.

Returning parameters from FinalBuilder 7 Action Lists

If I'm not mistaken, it seems that FinalBuilder 7's action list parameters only support input values. Is there any way I can simulate a workaround for return parameters? I do not want to store return parameters in a global temp variable or even a stack, because I'm calling the same action list multiple times asynchronously.
Here is sample of what I want to do. (Notice the shared use of the same action list)
Async Action Group
+-Action Group
| +-Run Action List - [Do Some Calculation]
| +-Replace variable A with return parameter from previous action list
+-Action Group
+-Run Action List - [Do Some Calculation]
+-Replace variable B with return parameter from previous action list
I'm currently using an INI file in the action list to save return values. The calling method passes a parameter to the action list specifying to which INI key to save to. The calling method then reads the value from the INI from the key.
Surely there has to be a more elegant way to do this?
I have never seen any way to return variables from action lists.
That would be an excellent suggestion to post in the FinalBuilder Wish List forum.
Many of requests in the past there are now features of the product.
I think it would require giving scope to variables to something like an Action Group to pull it off. But it would help some of my scripts as well. Update: I found that FB 7 supports Local Variables. But it still does not address the needs of this answer.