猿问

如何在 HTTP POST/GET 中发送空参数

在 http post/get 请求中发送空值的正确方法是什么。


重要提示:我的意思是为请求的特定参数发送NULL值,而不是空字符串或缺少字段。我认为以下选项不正确


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

在 HTTP 中发送NULL是否有意义(并且是标准化的)还是我应该回退到以前的任何选项?在后一种情况下,标准方法是什么


在java中,当我使用:


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

它崩溃了 Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference


使用OkHttp3 lib 并尝试添加空参数时,我也会崩溃


FormBody.Builder bodyBuilder = new FormBody.Builder();

bodyBuilder.add("param1",null)


ibeautiful
浏览 1009回答 2
2回答

小唯快跑啊

""如果您在这里添加空参数,只需使用空字符串而不是空参数,它将显示异常,因为它找到了一个空值来发送 http 请求
随时随地看视频慕课网APP

相关分类

Java
我要回答