54

I cannot work out how to pass a variable with a boolean value of false through a URL string. What is the correct method?

  • domain/page?test=false
  • domain/page?test=
  • domain/page?test=0
Benjen
  • 2,835
  • 5
  • 28
  • 42

4 Answers4

79

URLs are strings and all values in a URL are strings. When you see i=0 in a URL, 0 is a string. When you see b=true, true is a string. When you see s=, the value is an empty string.

For those strings to take on another meaning—the integer 0 or the Boolean true, say—the server-side program must be told how to interpret them. Often web frameworks will make intelligent guesses as to the right type, or sometimes a type is declared explicitly in a model.

How you'll do it depends entirely on what server-side language and what framework, if any, you're using.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
7

Like mentioned above, it's depends on the server-side language you are using as to how you will handle it. To add to the answer, if you want to pass a true or false in the URL as a parameter you can

running node on server

const whatever = JSON.parse(queryStringParameter)

running php on server

$whatever = json_decode(queryStringParameter)

Both options will give you a boolean type that you can use on the server.

3

It depends on how you interprete the the parameter string. You can write converter that can convert string values "false", "0", "" to false. You can take all approach, any one of them or mix.

sohan nohemy
  • 615
  • 5
  • 13
1

If you can make "1"=true and "0"=false, then converting the parameter value to Boolean will be easy in JavaScript, e.g. Node.js:

// !!1 = true;
// !!0 = false;

let test = !!parseInt(req.query.test);
Antonio Ooi
  • 1,601
  • 1
  • 18
  • 32