1

What is the correct approach for sending null values in http post/get requests.

Important: I'm meaning to send NULL value for an specific param of the request, not empty strings or missing fields. I considered the following options as incorrect

http://test.com?param1=&param2=bar      //this is an empty string, not a null
http://test.com?param1=null&param2=bar  //this is a string with content "null"
http://test.com?param2=bar              //param 1 is missing, not null

Does sending a NULL make sense in HTTP (and is standardised) or should I fallback to any of the previous options? In the latter case, whats is the standard approach

In java when I use:

URLEncoder.encode(null, "UTF-8")

It crashes with Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

I also get a crash when using OkHttp3 lib and try to add a null param

FormBody.Builder bodyBuilder = new FormBody.Builder();
bodyBuilder.add("param1",null)
Addev
  • 31,819
  • 51
  • 183
  • 302

2 Answers2

2

The only sensible option you did not disqualify in the question is parameter without value.

http://example.com?param1&param2&param3=foo

Though I would go for not adding the property at all to the query string, as it results in the value to be "not having a value" hich is what null usually means.

Torben
  • 3,805
  • 26
  • 31
1

Just use a blank "" string instead of null param if you are add null param here it will show exception because it find a null value to send http request

Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
  • 3
    That is equivalent to reserving a keyword for the null value as it removes a technically legal value from the possible value space. Using "NULL", "null", "nil" or "bobgeldof" as a replacement for null value is just as valid and is probably more readable (well, maybe except for Bob). – Torben Aug 21 '18 at 12:25