Trying to pass boolean variable to JSON gets 'Expected type boolean but found type string' error on POST

Hi there,

I’m trying to randomize a true/false boolean value, which I want to assign to a variable that I can then pass to my Body and post as part of my JSON. I tried this:

var arrayCert = [true, false];
pm.globals.set('certVal',  arrayCert[Math.floor(Math.random()*arrayCert.length)]);

Which does get me a random value in certVal . But when I try to pass that variable in my Body like so:

"Certified": "{{certVal}}",

I get this message:

"INVALID_REQUEST_PARAMETER: 'Certified' Expected type boolean but found type string; ",

I think the double quotes in the body is passing the variable as a string, hence the error. If I change it to:

"Certified": true,

Then the post works as expected. Is there anyway I can pass a random boolean in this way?

Thanks so much for any help.

Ed

@ed_truqc

as far as i know :thinking:
Boolean values weren’t denoted with double quotes.
if the Boolean value is double quoted, it is considered as string. (same happened in your case).

you can try this to pass the Boolean.

"Certified": {{certVal}},

@gopikrishna4595, thanks for your response. I have tried this, and it appears to be working when the value is true. But if the value if false, I get this error:

expected '{"error_msg":"Unexpected token , in JSON at position 109","error_debug":"DEBUG: ","error_type":""}' 

Any idea why false alone would cause this error?

I can actually see that when certVal is set to true, it gets passed as true:

\"Certified\": true,\n

but when certVal is set to false, it gets passed as a blank:

\"Certified\": ,\n

Thanks!
Ed

@ed_truqc
can you comment the tests and manually set certVal as false in Environments and execute ?

@gopikrishna4595 . It used to work but now it’s getting an error. I’ll check with my devs as there may be something wrong with our api.

Gaaah, sorry for all the edits. So if I manually set Certified to false, the post works correctly. If I change my array to false only, rather than true and false, then certVal is set to false, but Certified is set to blank:

\"Certified\": ,\n

@gopikrishna4595 Just re-read your post. Sorry for the confusion. I just tried setting certVal as an environment variable, set to false, and that worked!

{
      "key": "certVal",
      "value": "false",
      "enabled": true,
      "type": "text"
    },

I think I may have a problem with my api, as I’m seeing inconsistent results. Sometimes the post passes and sometimes it fails. I’ll investigate further on my end, but this does appear to be working now. Thanks again so much for your help and patience!

Ed

OK, I’ve changed my code that randomly picks a true/false value to include quotes, and set my certVal variable to be an environment variable:

"var arrayCert = ['true', 'false'];",
"pm.environment.set('certVal',  arrayCert[Math.floor(Math.random()*arrayCert.length)]);",

My postman_environment.json file now includes this:

{
      "key": "certVal",
      "value": "false",
      "enabled": true,
      "type": "text"
    },

And my POST body includes certVal in this format:

\"Certified\": {{certVal}},\n

And when I run my POST request, I do always get a true or false boolean passed as part of my post.

Thanks so much @gopikrishna4595 for the help on this!

Ed