Problem description
When you upload a file using the OSS Android software development kit (SDK), an error occurs if you set the callbackSNI parameter for a server callback. This error is caused by a type mismatch:
The OSS Android SDK defines the callbackSNI parameter as a string.

The server-side callback logic expects the callbackSNI parameter to be a Boolean value.

Passing the string "true" results in the error The callback sni is invalid.

Cause
JSON is a data exchange format without explicit types. As a result, when a Java object is serialized into a JSON string, its original type information, such as Boolean or string, is lost.
For OSS callbacks, the server requires the `callbackSNI` parameter to be a Boolean (`true` or `false`). Therefore, when you construct the callback parameters, you must ensure that the generated JSON represents this field as a Boolean, such as callbackSNI: true, and not as a string, such as "callbackSNI": "true".
Solution
When you construct upload callback parameters, ensure that `callbackSNI` is correctly serialized as a JSON Boolean value by following these steps:
Use
HashMap<String, Object>to build callback parameters.Set callbackSNI to the Java Boolean value
trueorfalse.Convert the Map to a
JSONObjectto ensure that types are serialized correctly.Convert the JSON object to a string and then Base64-encode the string.
Inject the encoded string into the upload request metadata using the
x-oss-callbackrequest header.
The following code provides an example:
Map callbackMap = new HashMap<String, Object>() {
{
put("callbackUrl", mCallbackAddress);
// The callbackBody can contain custom information.
put("callbackBody", "filename=${object}");
put("callbackSNI", true);
}};
JSONObject jsonObj = new JSONObject(callbackMap);
String callback = Base64.encodeToString(jsonObj.toString().getBytes(), Base64.NO_WRAP);
Log.d("oss", jsonObj.toString());
ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("x-oss-callback", callback);
put.setMetadata(metadata);