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

使用ThingsBoard规则链调用带表单URL编码体的REST API遇415错误求助

解决ThingsBoard规则链发送表单URL编码POST请求的415错误

问题核心在于:ThingsBoard的HTTP调用节点默认会将请求体按JSON格式处理,即便手动设置了application/x-www-form-urlencoded的Content-Type头,请求体仍然是JSON字符串格式,不符合API的表单编码要求,导致返回415错误。以下是具体解决步骤:

步骤1:添加脚本节点预处理请求体

在HTTP调用节点前新增一个脚本节点,通过JavaScript将JSON参数转换为表单URL编码的字符串:

// 定义认证参数
const authParams = {
    grant_type: "password",
    username: "XXX",
    password: "XXX"
};
// 转换为表单URL编码格式
const encodedBody = Object.keys(authParams)
    .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(authParams[key])}`)
    .join('&');
// 将处理后的请求体存入消息元数据
metadata.encodedFormBody = encodedBody;
// 设置Content-Type请求头到元数据
metadata['Content-Type'] = 'application/x-www-form-urlencoded';

return {msg: msg, metadata: metadata, msgType: msgType};

步骤2:配置HTTP调用节点

  1. 选择POST方法,填写API的认证令牌接口URL;
  2. 在请求体配置项中,选择「元数据」,然后选中脚本节点中存入的encodedFormBody字段;
  3. 若未在脚本中设置Content-Type,也可在HTTP节点的「请求头」中手动添加Content-Type: application/x-www-form-urlencoded。

步骤3:验证请求(可选)

开启ThingsBoard的调试日志,查看HTTP请求的实际内容,确认请求体格式为grant_type=password&username=XXX&password=XXX,而非原始JSON字符串。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:37:01