All Products
Search
Document Center

Object Storage Service:Error 'The callback sni is invalid' occurs during an upload callback with the OSS Android SDK

Last Updated:Nov 19, 2025

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.

    image

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

    image

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

image

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:

  1. Use HashMap<String, Object> to build callback parameters.

  2. Set callbackSNI to the Java Boolean value true or false.

  3. Convert the Map to a JSONObject to ensure that types are serialized correctly.

  4. Convert the JSON object to a string and then Base64-encode the string.

  5. Inject the encoded string into the upload request metadata using the x-oss-callback request 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);