Iām trying to understand how to pass a comma separated array of integers in to an API, via a csv data file.
Iām working with an API which will create users in our application
As part of this API we can assign User Access via Groups within the Application. These are defined with the āgroupsā element in the request body.
In a Non-Parameterized request, I pass in:
āgroups": [5008,5009],
The actual āValue Passedā in the Request Body, when Viewed in the Postman Console is
groups:
0:5008
1:5009
The request is successfully processed without error and results in the assignment of group IDās 5008 & 5009 being assigned to the user.
For the Parameterized request body for the āGroupsā element I have this:
āgroupsā: ["{{groups}}"],
My Variable is defined as follows:
groups:{{groups}}
In My CSV data file for a single value passed in as ā5008ā for the groups variable and the request is successfully processed
The actual āValue Passedā in the Request Body, when Viewed in the Postman Console is
groups:
0:ā5008ā
If I pass in ā5008, 5009ā (this should result in the assignment of group IDās 5008 & 5009 being assigned to the user.
The āgroups;ā element in the Request Body in the Postman Console is:
groups:
0:"5008,5009"
And I get the following error
Response Body:
type:"JsonReaderException"
reason:"Could not convert string to integer: 5008,5009. Path āgroups[0]ā."
details:"Newtonsoft.Json.JsonReaderException: Could not convert string to integer: 5008,5009. Path āgroups[0]ā
So Finallyā¦
How do I need to āFormatā the āgroupsā value in my csv data file, when it contains a comma separated array of āGroup IDāsā so that it is properly read by Postman so that it will be properly formatted in the request body that is passed in to my API.
Youāll need to have a separate delimiter in your CSV file:
5008|5009|5010
You can then set a new variable in your pre-request script which replaces the ā|ā with ā,ā: pm.globals.set("comma_sep_groups" , pm.variables.get("groups").replace(/\|/g,",")), and use āgroupsā: [{{comma_sep_groups}}] in your request body.
Use a JSON data file instead of CSV - youāll be able to store groups in a comma-separated format.
The solution provided did not work for us.
We had similar problems with using a JSON Formatted Data file both before and after adding the pre-request script.
No matter what we do with the pre-request script the data winds up as a string
Because it is a string it is not converted to an array of integers which results in the error:
type:"JsonReaderException"
reason:"Could not convert string to integer: 5008,4906. Path āgroups[0]ā.ā
details:āNewtonsoft.Json.JsonReaderException: Could not convert string to integer: 5008,4906. Path 'groups[0]'
Working with my developer we added a statement into the Pre-request script suggested, to output the contents of the groups field from the data file to the Console:
var x = pm.variables.get(āgroupsā).replace(/|/g,ā,ā).split(ā,ā);
pm.globals.set(ācomma_sep_groupsā, x);
console.log(x);
When I run the request through Runnerā It appears in the console as this:
Array:[]
0:"5008"
1:"4906"
Which is what would be expected and is also what we see in the request body when the request is passed in as non-parameterized.
IT appears that when the data is āsavedā back into the request body it appears to get flattened back into a string, when the request body is view in the console after the request has been run through āRunnerā it appears like this:
groups:
0:ā5008,4906ā
The Request body of a āGoodā request appears like this
groups:
0:ā5008ā
1:ā4906ā
The variable in this case needs to be read as an array not a string
Here, I have a global variable whose value is ā5008,5009ā.
Variables are always read as strings, but since they are replaced as-is in your request body, [{{groups}}] will resolve to [5008,5009] if groups is a string: 5008, 5009
Removing the .split(ā,ā) modification (/|/g, not /|/g) will let x be a string. Youāll need to remove the quotes inside the square braces in your request body for it to not be treated as a string.