Fast-Android-Networking icon indicating copy to clipboard operation
Fast-Android-Networking copied to clipboard

Post requests executed with incomplete Body Parameters when null value used!

Open AhmedAsser opened this issue 5 years ago • 1 comments

With okhttp:3.10.0 version which this library use, it throws an exception if any of fields name or value are null.

// FormBody Class
// add method
public Builder addEncoded(String name, String value) {
      if (name == null) throw new NullPointerException("name == null");
      if (value == null) throw new NullPointerException("value == null");

      names.add(HttpUrl.canonicalize(name, FORM_ENCODE_SET, true, false, true, true, charset));
      values.add(HttpUrl.canonicalize(value, FORM_ENCODE_SET, true, false, true, **true,** charset));
      return this;
    }

And when using for example:

HashMap<String, String> fieldVariables = new HashMap<String, String>();
fieldVariables.put("field1","field1");
fieldVariables.put("field2","field2");
fieldVariables.put("field3",null);
fieldVariables.put("field4","field4");
fieldVariables.put("field5","field5");
AndroidNetworking.put("https://example.con")).addBodyParameter(fieldVariables).build();

The request would be completed successfully but field4 and field5 did not send due to the previous okhttp exception.

//ANRequest Class
//getRequestBody Method
else {
            FormBody.Builder builder = new FormBody.Builder();
            try {
                for (HashMap.Entry<String, String> entry : mBodyParameterMap.entrySet()) {
                    builder.add(entry.getKey(), entry.getValue());  //<<<< break loop when trying add null value
                }
                for (HashMap.Entry<String, String> entry : mUrlEncodedFormBodyParameterMap.entrySet()) {
                    builder.addEncoded(entry.getKey(), entry.getValue()); // <<<< break loop when trying add null value
                }
            } catch (Exception e) {
                e.printStackTrace(); //<<<<< bad code
            }
            return builder.build();
        }

Which cause unexpected issues.

AhmedAsser avatar Oct 16 '20 17:10 AhmedAsser

We will improve this. Thank you for pointing it out.

amitshekhariitbhu avatar Oct 20 '20 10:10 amitshekhariitbhu