Postman- Use collection variable in request body - postman

I have a problem with Postman, where I want to use collection variables inside the request body.
According to postman documentation, all variables in postman GUI can be retrieved with double curly braces {{}}.
But it does not work for me. If I move variables from collection to environment, everything is working OK, but as soon as I move the variable from the environment to collection, it starts throwing errors like this:
JSONError: Unexpected token 'U' at 1:1
Unrecognized token 'Backend': was expecting (JSON String, Number, Array, Object
This is my body:
{
"name": {{BackendValidationPSName}},
"groups": {{myBackendValidationRGuuids}}
}
Can anyone point me in the right direction? Tx.

The values have to be in double quotes
{
"name": "{{BackendValidationPSName}}",
"groups": "{{myBackendValidationRGuuids}}"
}

Solved. I was missing the "" in the collection variable value.

Related

How to use mod_assign_save_grade?

I'm trying to use mod_assign_save_grade with : https:/[my-root]/webservice/rest/server.php?wstoken=[token]&wsfunction=mod_assign_save_grade&moodlewsrestformat=json&assignmentid=4&userid =45&grade =15&attemptnumber =-1&addattempt =0&workflowstate =graded&applytoall =1
but the output came with
{
"exception": "invalid_parameter_exception",
"errorcode": "invalidparameter",
"message": "Invalid parameter value detected"
}
any ideas to use it correctly
It seems there are blank spaces before = in several places. I copied your request, (of course, with actual server name, token and parameter values), removed blanks and it works correctly. When I insert blank space anywhere, the message that you posted appears.

How to use substring of a variable in a param or another variable?

I can use
{{guid}}
to get an id for body params that I can use for a new records ID.
How do I use a substring of guid in postman either in that param variable (which is collection level, by the way) or through another variable?
I tried various such as
{{$guid.substring(10)}}
{{$guid}.substring(10)}
but they did not work
I tried to use a pre-request script like this:
pm.collectionVariables.set("short_guid", pm.collectionVariables.get("myGUID").substr(12) )
using my collection variable myGUID which is defined as {{guid}}
along with
"email": "{{$pm.collectionVariables.get('myGUID')}}#example.com",
but it does not work as the variable is not interpreted and I get a string literal
you have to refer variable in body as {{<variablename}} don't need to call pm.varaible.get
for prerequest :
var uuid = require("uuid")
var myUUID = uuid.v4();
pm.environment.set("myuuid",myUUID.substring(20))
console.log(pm.environment.get("myuuid"))
And in body
{
"email": {{myuuid}}
}
you can also use variables in script section as:
console.log(pm.variables.replaceIn("{{$guid}}").substring(20))
This will first replace {{$guid}} with the postman variable value and then find the substring
you can use it like
console.log(pm.variables.replaceIn("this will print {{variable}} value"))
The second issue of variable not detecting is because of the variable scope
In postman variable scope is from right to left , but precedence is from left to right as follows:
Local>data>environment>collection>global
So if you have a environment variable and collection varible with same name , the value of that variable will be from environment not collection

Store Data from Postman request in variables to use in tests

Im currently trying to get used to POSTMAN and i was wondering if there is a way to store variables from my request JSON Body via Pre Request in some environment variable so ican resuse it in the tests for response value cheks
This is how my json File might look like
{
"text" : "myText",
"attachments": {
"text": "myText2",
"anotherText" : "myText3"
}
So i want to get all Values, store them in a variable before sending my request, and then test if they match the expected value in my response
(example: myText2 gets mapped to green, myText3 gets mapped to red and so on)
That would make it possible to write one test for several request
Thanks a lot!
You can write the following in your script:
let body = JSON.parse(pm.request.body);
_.forEach(body, (value, key) => pm.environment.set(key, JSON.stringify(value)));
This will set each key and it's associated value as an environment variables.
Note you'll need to JSON.parse the value in the test script before using it for testing.
For eg in your test script you'll need to do something like this:
let attachments = JSON.parse(pm.environment.get('attachments'));
pm.test('All attachments are of correct value', function () {
// ...write your test here using the `attachments` variable
});

How to read entire test data file in postman as part of pre-request script?

I am trying to read the entire test data file as a part of pre-request script in postman.
I tried the variable pm.iterationData, however ,it prints only the current iteration data set in the colletion runner.
I need the entire test data and load it as an environment variable in postman.
Is there a way?
The solution that i could find for this is, to set the test data in a variable as a part of pre-request script as follows:
let testdataset =
[
{
"name": "xyz",
"address": "abcd",
"value": "Hello"
},
{
"name" : "mno",
"address" : "defg",
"value" : "Mnop"
}
];
The best way I have come up with dealing with this (collecting all data from a file to us in one request) is to:
Have 2 nodes
The first node has
A dummy call to something like https://postman-echo.com/
Code that:
i. stores the table headers in an environment variable;
ii. concatenates the rows into environment variables;
iii. does 'postman.setNextRequest(null)' for all but the last row
The second node
Only runs in the last iteration
Sends collected data in the environment variable to the API
There is (currently) no way to not make any call on the first node at the moment.
See Github ticket for a request to do this: Request a way for nodes in collection to be logic-only, no request issued #5707

Postman - How can I pass array as variable

Is there the possibility to use an array variable inside postman?
e.g. inside the body of a request:
{
"myData" : {{arrayVariable}}
}
and inside the data file:
{
"arrayVariable": ["1", "2", "3"]
}
It's possible, you can even add your own keys
You can create a JSON body like this:
{
"myData" : [
{{arrayVariable}}
]
}
And the variable like this:
arrayVariable: "1", "2", "3"
where arrayVariable is the key and "1", "2", "3" is the value.
using variable with a same name will give you an array
Postman environment variables are meant to just save data as string, so here you are the workaround to pass array as environment variable/data file to Postman as a string like this:
{
"arrayVariable": '["1", "2", "3"]'
}
Then, add the following piece of code to parse this variable in pre-request script in Postman like this:
var x = JSON.parse(postman.getEnvironmentVariable("arrayVariable"));
postman.setEnvironmentVariable("arrayVariable", x);
Please create your body request like below
{
"myData" : ["{{arrayVariable}}"]
}
and there is no change required for data file.you can use as it is.
{
"arrayVariable": ["1", "2", "3"]
}
It will work definatly.
The only solution worked for me was something like the answer MickJagger provided, but I think it needs some clarifications.
The JSON data file should be something like this:
[
{
"anArray": "1, \"2\", 3.0, \"Foo\", false"
}
]
which it's value is a string, escaping the quotations for string elements.
(Note that this example differs from example provided by original question, to cover more use cases.)
The variables is as MickJagger said:
{
"value": [{{anArray}}]
}
Maybe other solutions works on previous postman versions, but this solution is tested on postman's latest version (by the time of publishing this answer), i.e. v7.34.0 .