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

将Curl转为Node.js Request时JSON格式错误问题求助

解决MessageBird Conversations API的JSON格式错误问题

转换Curl请求到Node.js request模块后,始终收到以下错误:

{ "errors": [{ "code": 21, "description": "JSON is not a valid format" }] }

原Curl请求

curl --location 'https://conversations.messagebird.com/v1/conversations/start' \
--header 'Authorization: AccessKey Key' \
--header 'Content-Type: application/json' \
--data '{"to": "+919853092550",
"type": "hsm",
"channelId": "f10ea05178db478089117cc7cba79d5c",
"content":
{"hsm": 
{"namespace": "92239cc8_406b_4bdf_aab5_b1c379b9b139",
"templateName": "otp",
"language": 
{"policy": "deterministic","code": "en"},
"params": [
    {"default": "123456"}
    ]
    }
    }}'

转换后的Node.js代码(存在问题)

var headers = {
  Authorization: "AccessKey Key",
  "Content-Type": "application/json",
};
var dataString = `{
  "to": ${number},
  "type": "hsm",
  "channelId": "f10ea05178db478089117cc7cba79d5c",
  "content": {
    "hsm": {
      "namespace": "92239cc8_406b_4bdf_aab5_b1c379b9b139",
      "templateName": "otp",
      "language": { "policy": "deterministic", "code": "en" },
      "params": [{ "default": ${otp} }],
    },
  },
}`;
var options = {
  url: "https://conversations.messagebird.com/v1/conversations/start",
  method: "POST",
  headers: headers,
  body: dataString,
};
request.post(options, function (error, response, body) {
  if (error) console.log(error);
  console.log(body);
});

问题排查与解决

核心问题

  1. 字符串变量未加引号:模板字符串中${number}和${otp}直接拼接,生成的JSON里这些字段值没有被引号包裹,不符合JSON语法(字符串必须用双引号)。
  2. 多余的尾逗号:params数组内的对象后、hsm对象后都有多余逗号,严格的JSON解析器会判定格式错误。
  3. 手动拼接JSON易出错:手动拼接字符串很容易遗漏格式细节,比如特殊字符转义、引号匹配等。

正确写法(推荐使用对象转JSON)

利用request模块的json: true参数,直接传入JavaScript对象,模块会自动处理JSON序列化和Content-Type设置,彻底避免手动拼接的问题:

var headers = {
  Authorization: "AccessKey Key"
};
// 构建请求体对象
var requestBody = {
  to: number, // 确保number是字符串类型,如"+919853092550"
  type: "hsm",
  channelId: "f10ea05178db478089117cc7cba79d5c",
  content: {
    hsm: {
      namespace: "92239cc8_406b_4bdf_aab5_b1c379b9b139",
      templateName: "otp",
      language: { policy: "deterministic", code: "en" },
      params: [{ default: otp }] // otp需为字符串类型
    }
  }
};
var options = {
  url: "https://conversations.messagebird.com/v1/conversations/start",
  method: "POST",
  headers: headers,
  json: true, // 自动序列化body为JSON,设置Content-Type
  body: requestBody
};
request.post(options, function (error, response, body) {
  if (error) console.log(error);
  console.log(body);
});

若坚持使用模板字符串的修正方案

给变量添加引号,并移除多余尾逗号:

var dataString = `{
  "to": "${number}",
  "type": "hsm",
  "channelId": "f10ea05178db478089117cc7cba79d5c",
  "content": {
    "hsm": {
      "namespace": "92239cc8_406b_4bdf_aab5_b1c379b9b139",
      "templateName": "otp",
      "language": { "policy": "deterministic", "code": "en" },
      "params": [{ "default": "${otp}" }]
    }
  }
}`;

注意:此方式需确保number和otp中不包含未转义的双引号,否则会破坏JSON结构,不如对象转JSON的方式可靠。

内容的提问来源于stack exchange,提问作者Kiran S youtube channel

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 08:47:37