javascript - Make postman query param type of boolean - Stack Overflow

I use postman to test my apihttp:localhost:3000apibooks?disabled=trueI want to set disabled=true (o

I use postman to test my api

http://localhost:3000/api/books?disabled=true

I want to set disabled=true (or false) and I expect it is a bool

But somehow

const {disabled} = req.query;
console.log(typeof disabled) //I get disabled type of string

How I set my query param boolean type?

I use postman to test my api

http://localhost:3000/api/books?disabled=true

I want to set disabled=true (or false) and I expect it is a bool

But somehow

const {disabled} = req.query;
console.log(typeof disabled) //I get disabled type of string

How I set my query param boolean type?

Share Improve this question asked Nov 9, 2018 at 6:51 coinhndp coinhndp 2,49111 gold badges38 silver badges71 bronze badges 5
  • Have you tired to set disabled to 1 or 0 ? – Marcia Ong Commented Nov 9, 2018 at 6:53
  • It still shows that type of disabled is string –  coinhndp Commented Nov 9, 2018 at 6:54
  • codippa./how-to-convert-string-to-boolean-javascript – Marcia Ong Commented Nov 9, 2018 at 6:55
  • Just asking, won't it work with a boolean? like let condition = disabled==='true'? – lloydaf Commented Nov 9, 2018 at 6:56
  • You will need to parse it in the backend. – Marcia Ong Commented Nov 9, 2018 at 7:01
Add a ment  | 

3 Answers 3

Reset to default 0

Regardless of the format (query params, JSON body) it is the job of the backend to parse and validate the request.

So even if you send a parameter like “active=true”, it is still a string, this is how the HTTP protocol works.

For more info ---> https://munity.getpostman./t/how-to-pass-boolean-values-using-postman/1174

let disabled = ( req.query.disabled !== 'false' )

That should do it. As explained in other answers, HTTP does not natively support any non-string or non-stream types (integers, booleans, etc.). The server is tasked with parsing the meaning from the original request format.

Alternatively, you can also do the following to bee more familiar with the intricacies of parsing JSON-based requests.

let disabled = JSON.parse(req.query.disabled)

Here is the screen print of passing boolean as query param

For any reason, if the param returns false, the issues could be that I came across,

  1. The key defined in @RequestParam has typo
  2. The key defined in @RequestParam has space
  3. The key defined in @RequestParam not matching with the key mentioned in postman query param list

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744968217a4603813.html

相关推荐

  • javascript - Make postman query param type of boolean - Stack Overflow

    I use postman to test my apihttp:localhost:3000apibooks?disabled=trueI want to set disabled=true (o

    23小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信