Document Moderation Result Callback

Overview

The document moderation service can push the asynchronous moderation result to your callback URL after the task is completed or failed. You need to provide an HTTP endpoint that follows this specification.

You can enable callback in either of the following ways:

  • Configure a callback URL in <Console - Service Configuration>.
  • Pass callbackUrl when calling the Document Moderation Task Submission API. The request-level callbackUrl takes precedence over the callback URL configured in the console.

Callback Request

  • Request URL:

The callback URL configured by you or passed in the submit request.

  • Request method:

POST

  • HTTP request Header:
Header Value Description
Content-Type application/json Callback request body format
signature ***** Signature used to verify the validity of the request. See the authentication algorithm

Interface Authentication

Client can perform interface authentication through the key information assigned after configuring callback parameters. The specific method is as follows:

  • Signature Algorithm

    1> Sort the received request body parameters in ascending order of the ASCII code table.

    2> Append the sorted parameters into a string. The format is: key1 + value1 + key2 + value2....

    3> Append the key generated after configuring callback parameters to the string generated in step 2>.

    4> Encode the string generated in step 3> with UTF-8, calculate the MD5 hash to generate the signature value, and put it into the request Header.

  • Example code

/**
 * Generate signature
 * @param secretKey key generated in Console-Service Configuration
 * @param params interface request parameter name and parameter value map, excluding signature parameter name
 * @return
 */
public static String signature(String secretKey, Map<String, String> params) {
    String[] keys = params.keySet().toArray(new String[0]);
    Arrays.sort(keys);

    StringBuilder signBuilder = new StringBuilder();
    for (String key : keys) {
        signBuilder.append(key).append(params.get(key));
    }
    signBuilder.append(secretKey);

    try {
        return DigestUtils.md5Hex(signBuilder.toString().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // error
    }
    return null;
}

After a callback secret key is configured in the console service configuration, the callback request body uses the following structure:

Field name Type Description
appId String Project identifier
taskId String Unique identifier of the task
result JSON String Moderation result. See “Callback result JSON” below for the field structure

If no callback secret key is configured, the service directly pushes the result in the “Callback result JSON” structure below and does not send signature.

  • Callback result JSON:
Field name Type Description
errorCode Number Error code. 0 means success
errorMessage String Error message
code Number Task status code: 0 completed, 1 failed, 2 processing, 3 invalid taskId
taskId String Unique identifier of the task
appId String Project identifier
inputType String Input type. Document moderation returns DOCUMENT
documentType Number Document type used by the service. 0 unknown, 1 PDF, 2 CSV, 3 XLSX, 4 XLS, 5 DOCX, 6 DOC
strategyId String Strategy ID used by this task
result Number Document-level moderation result: 0 pass, 1 suspected, 2 block
items Array Moderation results of parsed document items

items:

Field name Type Description
itemId String Unique identifier of the parsed item
mediaType String Media type: TEXT or IMAGE
taskId String Provider task ID for asynchronous media items. This field may be empty for sync items
sourceUrl String Source URL of an image item
originalText String Original text content. Returned for text items
filteredText String Filtered text content. Returned when a text item hits blocking content
result Number Item-level moderation result: 0 pass, 1 suspected, 2 block
tags Array Hit category information

tags:

Field name Type Description
tag Number First-level category code. For details, see the classification code tables in Help Center
subTags Array Second-level category information

subTags:

Field name Type Description
subTag Number Second-level category code
  • Callback request example:
{
  "errorCode": 0,
  "code": 0,
  "taskId": "task_**************************",
  "appId": "82100001",
  "inputType": "DOCUMENT",
  "documentType": 1,
  "strategyId": "DEFAULT",
  "result": 2,
  "items": [
    {
      "itemId": "document_text_1",
      "mediaType": "TEXT",
      "originalText": "Example document text",
      "filteredText": "Example **** text",
      "result": 2,
      "tags": [
        {
          "tag": 150,
          "subTags": [
            {
              "subTag": 150001
            }
          ]
        }
      ]
    },
    {
      "itemId": "document_image_1",
      "mediaType": "IMAGE",
      "result": 0
    }
  ]
}
  • Callback request example with signature enabled:
{
  "appId": "82100001",
  "taskId": "task_**************************",
  "result": "{\"errorCode\":0,\"code\":0,\"taskId\":\"task_**************************\",\"appId\":\"82100001\",\"inputType\":\"DOCUMENT\",\"documentType\":1,\"strategyId\":\"DEFAULT\",\"result\":2,\"items\":[]}"
}

Callback Response

After receiving the callback request, your endpoint must return a JSON response. A response with code: 0 is treated as successful.

Field name Type Required Description
code Number Required Response code. 0 means the callback was received successfully
message String Optional Response message
  • Response example:
{
  "code": 0,
  "message": "success"
}

If the response body is empty, the response code is not 0, the request times out, or the callback endpoint returns an error, the service will retry the callback. The retry interval starts at 1 second, and the service retries up to 3 times.