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.