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

Bash curl向Bitbucket PR评论发送POST请求返回Bad Request

解决Bitbucket API curl请求Bad Request问题

你的问题出在bash解析带特殊字符和换行的变量时,手动拼接JSON会导致格式错误,Bitbucket无法识别。结合你Postman的成功案例,给你两个可靠的解决方法:

方法一:用jq自动构造合法JSON(推荐)

jq能自动处理变量中的特殊字符、引号、换行,生成标准JSON,避免手动转义的麻烦。

安装jq后,执行以下命令:

curl -S https://api.bitbucket.org/2.0/repositories/etc/etc/pullrequests/comments \
  -u username:pw \
  -X POST \
  -H 'Content-Type: application/json' \
  -d "$(jq -n --arg log "$logMsg" '{content: {raw: $log}}')"
  • --arg log "$logMsg":把$logMsg的值传递给jq的$log变量,自动完成转义
  • 用raw字段匹配你Postman中的成功请求,支持Markdown格式(比如你示例中的***HelloThere***)

方法二:手动转义变量内容(无jq时使用)

如果无法安装jq,需要手动转义$logMsg中的特殊字符,确保JSON格式合法:

# 转义双引号和换行符,其他特殊字符如*、<等在JSON字符串中无需额外转义
escaped_logMsg=$(printf '%s' "$logMsg" | sed -e 's/"/\\"/g' -e 's/\n/\\n/g')

# 用双引号包裹整个JSON字符串,确保bash正确解析变量
curl -S https://api.bitbucket.org/2.0/repositories/etc/etc/pullrequests/comments \
  -u username:pw \
  -X POST \
  -H 'Content-Type: application/json' \
  -d "{\"content\": {\"raw\": \"$escaped_logMsg\"}}"

额外注意事项

  • 修正了你原命令中的语法错误:-u username:pw行末尾需要加反斜杠,否则bash会把后续参数当成单独命令
  • 替换原命令中的text字段为raw,和你Postman的成功请求保持一致,确保Markdown格式生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 00:33:31