Updated on 2023-11-20 GMT+08:00

Text Moderation (V3)

  • Sample code for a Python 3 request
    # encoding:utf-8
    import requests
    import base64
    
    url = "https://{endpoint}/v3/{project_id}/moderation/text"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    data= {
      "event_type": "comment",
      "data": {
        "text": "test"
      }
    }
    response = requests.post(url, headers=headers, json=data, verify=False)
    print(response.text)
  • Sample code for a Java request
    //Add the following dependency before use. (The version is only an example.)
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.14.7</version>
    </dependency>
    
    package com.huawei.ais.demo;
    import java.io.IOException;
    
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
    
    /**
     *  This demo is used only for tests. You are advised to use the SDK.
     *  Add the okhttp3 dependency before use.
     */
    public class TextModerationDemo {
    
        public static void main(String[] args) throws IOException {
            try {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType, "{\"event_type\":\"comment\",\"glossary_names\":[],\"data\":{\"text\":\"test\"}}");
                Request request = new Request.Builder()
                        .url("https://{endpoint}/v3/{project_id}/moderation/text")
                        .method("POST", body)
                        .header("X-Auth-Token", "Actual token value obtained by the user")
                        .build();
                Response response = client.newCall(request).execute();
                String string = response.body().string();
                System.out.println(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • Sample code for a PHP request
    <?php
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://{endpoint}/v3/{project_id}/moderation/text',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => '{
          "event_type": "comment",
          "data": {
            "text": "test"
          }
        }',
        CURLOPT_HTTPHEADER => array(
            'X-Auth-Token: Actual token value obtained by the user',
            'Content-Type: application/json'
        ),
    ));
    
    $response = curl_exec($curl);
    
    curl_close($curl);
    echo $response;
    Table 1 Parameter description

    Parameter

    Description

    url

    API request URL, for example, https://{endpoint}/v3/{project_id}/moderation/text.

    endpoint indicates the endpoint, and project_id indicates the project ID.

    token

    A token is a user's access credential, which includes user identities and permissions. When you call an API to access a cloud service, a token is required for identity authentication.

    For details about how to obtain the token, see Authentication.