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

如何在HPCC ECL中使用SendGrid API发送POST请求?

在HPCC ECL中调用SendGrid API发起POST请求

HPCC ECL 可借助内置的 HTTPClient 工具类发起HTTP请求,以下是对接SendGrid发送邮件API的具体实现方案:

前置准备

  • 有效的SendGrid API密钥(从SendGrid控制台获取)
  • 确认HPCC集群能正常访问SendGrid的API域名(api.sendgrid.com)

实现代码

IMPORT HTTP;

// 配置SendGrid核心参数
sg_api_key := '你的SendGrid API密钥';
sg_endpoint := 'https://api.sendgrid.com/v3/mail/send';

// 构造请求头:必须包含认证和格式声明
request_headers := HTTP.CreateHeaders(
  'Authorization', 'Bearer ' || sg_api_key,
  'Content-Type', 'application/json'
);

// 构造邮件请求体(严格遵循SendGrid的JSON格式规范)
request_body := '{
  "personalizations": [
    {
      "to": [{"email": "recipient@example.com"}],
      "subject": "HPCC ECL SendGrid测试邮件"
    }
  ],
  "from": {"email": "sender@example.com"},
  "content": [
    {
      "type": "text/plain",
      "value": "这是通过SendGrid API发送的测试内容"
    }
  ]
}';

// 发起POST请求
response := HTTP.HTTPClient.Post(sg_endpoint, request_body, request_headers);

// 输出响应结果,用于调试验证
OUTPUT(response, NAMED('SendGrid_Response'));

关键注意事项

  • 请求头规范:Authorization 字段必须是 Bearer + 你的API密钥格式,Content-Type 固定为 application/json,不符合会触发权限或格式错误。
  • 请求体格式:必须严格匹配SendGrid API的字段要求,比如personalizations、from、content等核心字段不能缺失或拼写错误,否则会返回4xx类错误。
  • 响应校验:成功请求会返回202 Accepted状态码,可通过response.StatusCode快速判断;若请求失败,response.Content会包含详细错误信息,便于排查问题。
  • 安全建议:不要直接硬编码API密钥,建议通过HPCC的安全配置参数或加密存储方式引入,避免密钥泄露。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 02:12:12