POST -打标签
介绍打标签接口的接口调用方式。
请求方式
                    POST
                
            POST请求说明
一条完整的POST请求应当包含以下4个参数内容:
        - url:请求的地址。
 - headers:请求的HTTP标头。
 - data:发送的metadata信息,示例格式为:
metadata = { "app": "appA", "queryID": str(uuid.uuid4()), "update": True, "sendBack": {"type": "response"}, "labels": { "employID": "12340017", } } - files:待操作的本地文件。
 
注: 完整的代码示例,请查阅下方的Python请求示例
请求URL
https://<ucwi_endpoint>:5443/skg/v1/dlp/label/custom/<请求模式>| 参数名称 | 参数位置 | 是否必须 | 描述 | 
|---|---|---|---|
| <请求模式> | url | 是 | 
                            
  | 
                    
请求Header
请求Body中的metadata
| 参数名 | 类型 | 适用状态 | 说明 | 
|---|---|---|---|
| labels | Dict(必选) | 同步和异步 | 增加的自定义标签内容 | 
| app | String(必选) | 同步和异步 | WebService应用ID | 
| update | Boolean(可选) | 同步和异步 | 是否更新已有标签,默认为False | 
| sendBack | Dict(必选) | 同步和异步 | 打标签后返回文件的处理方式,支持本地返还、http上传,以及亚马逊S3、Swfit等云存储。
  | 
                    
| queryID | String(必选) | 同步和异步 | sync/async (同步或异步模式) | 
| uploadtype | String(必选) | 异步 | 只限于异步。支持AWS S3、阿里云OSS、腾讯云COS、Swift等云存储。
  | 
                    
| callback_url | String(必选) | 异步 | 只限于异步模式必须填写回调函数的url | 
| objectInfo | Dict(可选) | 异步 | 只限于异步模式并使用对象存储时使用。异步模式支持审查存储在亚马逊S3、Swfit、阿里云OSS、腾讯云COS等云服务的对象存储。具体设置参数,参阅章节:异步模式下审查云服务存储内容的objectinfo参数 | 
同步请求参数示例
{   
    "app": "875a2442-2fe6-49a5-abf7-00fbd727e99e",
    "queryID": "8a0aa812-0f80-4d25-8e3f-60583bac8b96",
    "update": True,
    "labels": {
        "test_key": "tt_value",
        "id": "8888"
    },
    "sendBack": {
        "type": "response"
    }
}
            异步请求参数示例
{
    "labels": {
        "test_key": "tt_value",
        "id": "8888"
    },
    "app": "875a2442-2fe6-49a5-abf7-00fbd727e99e",
    "update": True,
    "queryID": "8a0aa812-0f80-4d25-8e3f-60583bac8b96",
    "callback_url": "http://172.11.9.1:8000/callback",
    "sendBack": {
        "type": "s3",
        "s3Bucket": "leo",
        "s3Region": "",  # 使用s3时的region name,如果运行在EC2上并使用AWS IAM role,则以下三项可省略
        "accessKey": "admin",
        "secretKey": "admin",
        "endpointUrl": "http://172.20.162.160:9000",
    },
    "uploadtype": "file"
}
            Python请求示例 - 打标签
以下示例为使用Python代码调用接口,为本地文件打标签。
# -*- coding: utf-8 -*- from requests.packages.urllib3.exceptions import InsecureRequestWarning from ucwi_config import UCWIConfig from ucwi_auth import get_headers import requests import json import os import uuid import time from requests_toolbelt.multipart import decoder requests.packages.urllib3.disable_warnings(InsecureRequestWarning) api = "/skg/v1/dlp/label/custom/sync" url = "{0}{1}".format(UCWIConfig.base_url, api) file_path="CustomLabelTest.docx.RET.RET" metadata = { "app": "appA", "queryID": str(uuid.uuid4()), "update": True, "sendBack": {"type": "response"}, "labels": { "employID": "12340017", "Student-name": "peter", "BBB-XXX": "2222222", "EEE-XXX": "555555", } } headers = get_headers() data = {"metadata": json.dumps(metadata)} fd = open(file_path, 'rb') files = { "request": fd } response = requests.post(url, headers=headers, data=data, files=files, verify=False) fd.close() if response.status_code != 200: print("Bad request, response code:", response.status_code) print(response.text) else: if response.headers['Content-Type'] == 'application/json': info = response.json() print(info) elif response.headers['Content-Type'].startswith('multipart/form-data'): multipart_data = decoder.MultipartDecoder.from_response(response) for part in multipart_data.parts: content = part.content content_type = part.headers[b'content-type'] content_disposition = part.headers[b'content-disposition'] if content_type == b"application/json": info = json.loads(content) print("multipart info:{}".format(info)) elif content_type == b"application/octet-stream": cd_info = [x.strip() for x in content_disposition.decode('utf-8').split(';')] print("multipart xxx {}".format(cd_info)) for item in cd_info[1:]: index = item.find('=') key, value = item[:index], item[index + 1:] if key == 'filename': filename = value.strip('"') + ".RET" with open(filename, 'wb') as f: f.write(content)
错误代码
若调用出现错误,将返回以下错误代码:
        | 错误码 | 描述 | 
|---|---|
| 400 | 无效参数 | 
| 404 | 未知错误,例如请求了未触发的事件等。 | 
| 500 | 无效参数 |