You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求提供APAMA脚本实现Cumulocity POST API添加子资产示例

APAMA脚本实现Cumulocity子资产添加请求

实现思路

  • 用APAMA内置的HTTPClient组件发起POST请求,这是处理外部HTTP调用的标准方案
  • 替换API路径里的{device-id}为目标设备的实际ID,同时把Payload占位符换成待添加的托管对象ID
  • 配置Cumulocity认证:采用Basic Auth方式,将用户名和密码做Base64编码后放入Authorization请求头
  • 设置Content-Type: application/json请求头,保证服务端能正确解析JSON格式的请求体
  • 增加响应监听逻辑,判断请求执行结果,方便后续排查问题

代码片段

// 导入依赖包
import com.apama.http.HTTPClient;
import com.apama.http.HTTPRequest;
import com.apama.http.HTTPResponse;
import com.apama.json.JSON;
import com.apama.util.Base64;

monitor AddChildAssetMonitor {
    action onload() {
        // 初始化HTTP客户端
        HTTPClient httpClient := HTTPClient.create();
        
        // 替换为实际业务参数
        string targetDeviceId := "DEVICE_ID_HERE";
        string targetManagedObjId := "MANAGED_OBJECT_ID_HERE";
        string cumuUsername := "YOUR_USERNAME";
        string cumuPassword := "YOUR_PASSWORD";
        
        // 拼接完整API地址
        string apiEndpoint := "https://cti.ram.m2m.telekom.com/inventory/managedObjects/" + targetDeviceId + "/childAssets";
        
        // 构造请求体JSON
        string requestBody := JSON.stringify({
            "managedObject": {
                "id": targetManagedObjId
            }
        });
        
        // 生成Basic Auth头
        string authHeader := "Basic " + Base64.encode(cumuUsername + ":" + cumuPassword);
        
        // 构建POST请求
        HTTPRequest postReq := HTTPRequest.create(apiEndpoint, HTTPRequest.POST);
        postReq.addHeader("Content-Type", "application/json");
        postReq.addHeader("Authorization", authHeader);
        postReq.setBody(requestBody);
        
        // 发送请求并监听响应
        send postReq to httpClient.requestChannel;
        on HTTPResponse(resp) from httpClient.responseChannel where resp.requestId = postReq.requestId {
            if resp.statusCode in [200, 201] {
                log "子资产添加成功,响应内容: " + resp.body to "INFO";
            } else {
                log "子资产添加失败,状态码: " + resp.statusCode + ",错误信息: " + resp.body to "ERROR";
            }
        }
    }
}

注意事项

  • 确保APAMA项目已引入http、json及util相关依赖包
  • 若担心密码明文风险,可提前在外部生成Base64编码的认证串,替换代码中动态生成的部分
  • Cumulocity通常返回201表示创建成功,部分场景下也会返回200,需根据实际接口规范调整判断逻辑

内容的提问来源于stack exchange,提问作者arijit158

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:32:36