How to send NULL in HTTP query string?

or just don't send any value. leave query param value empty in your get request url.. like -

API URL/?foo=&some_other_param=value

in the API foo will be received as null


In your use case that is more clear after the edit I would go surely for the ?_json= parameter, there is nothing wrong with it and since everything works for POST as you write there is no doubt that is better than inventing a generic binding protocol through request parameters (application/x-www-form-urlencoded). I personally have been using two parameters to support also other content types like XML, one $rqc carries the content like your _json and one $rqct carries the content-type, eg:application/json

Said that, I think JSON works well because it allows 4 possibilities for a string property:

  1. filled {"param":"foo"}
  2. empty {"param":""}
  3. absent {}
  4. null {"param":null}

A request parameter instead can have only 3:

  1. filled ?param=foo
  2. empty ?param= or ?param (Server API usually does not differentiate the 2 cases)
  3. absent ?

The 4th possibility, an explicit null, can be handy for partial updates: if one updates only the fields present in the request JSON object; you cannot easily express that with a request parameter.

However if you don't do partial updates you may consider an absent parameter as a null value, but you need to know all parameters allowed for that endpoint, that is not always possible without a schema or other specification (a class to bind to); since you say that you associate parameters to types you probably have it.

The fact that ASP.NET (I learn it now from the answers) does not differentiate between empty and absent parameter reminds me that is often dangerous to allow an empty string and/or treat it differently from an absent/null string, for example because for Oracle an empty string is converted to null, so is not valid for a NOTNULL column (in mysql is ok), see null vs empty string in Oracle.

So if you don't need to support the difference between empty and null/absent string you may also consider the empty parameter as null.

If instead you want to recreate point 4. of JSON {"param":null} with request parameters you must use a convention, like passing %00 as suggested in the answers or using the null marker parameter I propose later.

If the purpose is to create something generic I wouldn't use %00 because some servers may have some parameters sanitization mechanism enabled: if it refuses %00 (as it probably should) then the convention can not work. I checked now and JSOUP removes all in the range %00-%0F. Maybe also ASP.net does by default, see Special character causes "A potentially dangerous Request.Form value"

A more HTML-ish approach could be using an extra null marker parameter whose name is obtained by the original parameter with a suffix, eg: from param it could be param$null: if the parameter is present then the bound value is null, whatever the value of param, it is like putting a unset checkbox before a text input and un-setting the bound value when is checked. Furthermore if you have some way to annotate a parameter/property to support this syntax you might limit the check to those parameters (probably less than 1% of total) and for all the others treat empty parameter and absent parameter in the same way: binding it to a null value.


Send a URLEncoded NULL value (%00) for any thing that you're specifically setting to null. It should be correctly URL Decoded.

Tags:

Http

Standards