代码之家  ›  专栏  ›  技术社区  ›  Addev

如何在http post/get中发送空参数[重复]

  •  0
  • Addev  · 技术社区  · 7 年前

    这个问题已经有了答案:

    在HTTP POST/GET请求中发送空值的正确方法是什么?

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

    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中有意义(并且是标准化的),或者我应该退回到前面的选项中吗?在后一种情况下,什么是标准方法

    在Java中,当我使用:

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

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

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

    FormBody.Builder bodyBuilder = new FormBody.Builder();
    bodyBuilder.add("param1",null)
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Torben    7 年前

    您在问题中没有取消资格的唯一明智的选择是没有值的参数。

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

    尽管我不打算把这个属性添加到查询字符串中,因为它会导致值“没有值”,这通常是空值的意思。

        2
  •  1
  •   Sandeep Parish    7 年前

    只需使用空白 "" 字符串而不是空参数如果在此处添加空参数,它将显示异常,因为它找到一个空值来发送HTTP请求