Can I urlencode null when sending request?
Asked Answered
G

4

8

I'm trying to send a request that origins from an object that contains null values:

ajax_post("path", {name: "Name", status: null}, callback);

I have my own method that URL encodes the objects. null is a javascript type object, so it ends up here:

  for(var i in data) {
    ...
    if(typeof data[i]=="object") {
      if(data[i]==null) {
        //HOW TO ENCODE THE NULL VALUE?
      }
      else 
      ...
    }
    else 
    ...
  }

The resulting $_POST on server side should contain these:

array(
    name => "Name",
    status => NULL
)

Is this possible or will I have to convert "NULL" into NULL manually on server side?

Gerhardine answered 2/10, 2014 at 10:53 Comment(7)
so you want to preserve the data type null when passsed on the query string? so that when the PHP receives it, the data type is still null?Tectonic
Exactly as you say it. Basically it's a question whether null is in URL specification or if I'll have to create my own protocol/syntax for it.Ferne
When you receive $_POST vars, the first thing you usually do is using isset(). I'd leave out null values and assume !isset == null. Like: if (!isset($_POST['status'])) { $status = null; }Dervish
The problem in my case is, that I'm using the null field to actually set something to null. So it would be most straightforward to keep the value. Also, I use the key to detect what request I'm handling. Otherwise, you're right.Ferne
@TomášZato: Maybe you could use the empty string instead of NULLPsalms
i don't think to get to keep that exact data type null, name=Name&status= you'll just get an empty string when it transports and becomes a query stringTectonic
Short answer? No.Orcein
O
1

You could simply not send the parameter that is to be null.
In the backend you have probably some clause that evaluates if the Parameter exists.
If the parameter does not exist you have your null

For your example:

for(var i in data) {
  ...
  if(typeof data[i]=="object") {
    if(data[i]==null) {
      //Dont send parameter so backend evaluates this to null
      //unset(data[i]);
    } else 
    ...
  } else {
    ...
  }
}

Ochlocracy answered 6/2, 2020 at 11:50 Comment(0)
O
0

You can use prefixes to differentiate null v. non-null.

So, if the variable is null, send "0", and if the variable is non-null, send "1". Then, when you receive a request:

  • verify the parameter is set and is not an empty string,
  • verify the first character is in the set of allowed values ('0' or '1'), and
  • strip the first character from the string and switch on that character, interpreting the remainder of the string accordingly
Orcein answered 30/3, 2016 at 15:7 Comment(3)
The aim of the question was to send something that doesn't require additional processing on server and something unambiguous.Ferne
@TomášZato Well, the above-defined grammar is unambiguous. But by definition query string parameters are just strings. If he wants he can write a function which does the lexing for him, but null is a language construct, not a string, so there must be postprocessing. There is, of course, the null byte %00, and I am kinda curious how PHP handles that, but I'd be willing to bet money sending simply %00 means server-side code just sees an empty string.Orcein
The requirement was no post-processing. If this is not possible then no is an answer, although alternatives are welcome for future visitors of this question. The fact that url parameters are strings does not mean a language construct cannot be implemented there - after all PHP arrays can be sent over query strings. It solely depends on how query strings are implemented on PHP servers, which is what this question is about.Ferne
O
0

In Javascript you may use \0 which encodes to %00 when using one of the encodeURI* methods:

console.log(encodeURI("\0"));

In PHP this would result in:

string(1) ""

Notice the one byte, in comparison to zero bytes for an 'real' empty string.

Odeliaodelinda answered 20/1, 2022 at 21:12 Comment(1)
Ok this is not what I meant but it's pretty neat. Bet a trying this in web apps would break some of them.Ferne
F
-3
 for(var i in data) {
    ...
    if(typeof data[i]=="object") {
      if((data[i]==NULL) || (empty(data[i]))) {
        //HOW TO ENCODE THE NULL VALUE?
    // echo base64_encode(data[i]);
    // echo base64_​decode(data[i]);
     echo 'NULL';
      }
      else 
      ...
    }
    else 
    ...
  }

Make sure all or NULL's are uppercase as-well. Else they will not be a valid NULL variable.

* Not Tested *

Ferritin answered 3/10, 2014 at 23:25 Comment(1)
In which language is your untested example written? What is it supposed to do?Ferne

© 2022 - 2024 — McMap. All rights reserved.