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

Linux下使用wget发送JSON格式POST请求及向Dashboard推送数据的可行性

当然没问题!wget完全能在Linux系统里发送带JSON格式数据的POST请求,我帮你把实现方法拆解清楚,包括针对你给出的特定JSON结构的具体命令。

用wget发送JSON POST请求的核心要点

要让服务器正确识别JSON格式的POST数据,有两个关键:

  1. 用--post-data(或--body-data,两者功能一致)传递JSON字符串
  2. 必须设置Content-Type: application/json请求头,否则服务器会默认按表单格式解析,无法识别JSON
针对你提供的JSON结构的具体命令

你的JSON里包含嵌套的转义JSON字符串,还有$message、$scriptname两个变量,这里给出两种实用的实现方式:

方式1:直接在命令行传递JSON

这种方式适合临时执行的场景,注意单双引号的搭配(避免shell解析变量和转义字符出错):

wget --header="Content-Type: application/json" \
     --post-data='{
         "data":"{\"actiontodo\":\"Action to do for test nr: 1\",\"critical\":\"LOW\",\"fixstatus\":\"NOTCONCERN\",\"host\":\"MTR_SOME_HOST\", \"message\":\"'"$message"'\",\"mgsApplication\":\"MTR\",\"sMxtype\":\"PROD\", \"scriptname\":\"'"$scriptname"'\"}",
         "msg":"NotificationReceiveDTO without dict to send at: 2020-06-07T11:14:09.794 Created at: 2020-06-07T11:14:09.797",
         "msgType":"DATA"
     }' \
     https://your-dashboard-endpoint-url.com/push

关键细节解释:

  • 外层用单引号包裹整个JSON,防止shell解析内部的双引号;遇到$message和$scriptname时,临时闭合单引号、用双引号包裹变量(避免变量含空格时出错),再重新打开单引号
  • 嵌套在data字段里的JSON保持原有的转义格式(\"),确保被Dashboard正确解析

方式2:通过文件传递JSON(更易维护)

如果JSON结构复杂或需要重复使用,建议把内容写入文件,再用--post-file参数发送:

# 1. 先将JSON模板写入文件,自动替换变量
cat > dashboard_payload.json << EOF
{
    "data":"{\"actiontodo\":\"Action to do for test nr: 1\",\"critical\":\"LOW\",\"fixstatus\":\"NOTCONCERN\",\"host\":\"MTR_SOME_HOST\", \"message\":\"$message\",\"mgsApplication\":\"MTR\",\"sMxtype\":\"PROD\", \"scriptname\":\"$scriptname\"}",
    "msg":"NotificationReceiveDTO without dict to send at: 2020-06-07T11:14:09.794 Created at: 2020-06-07T11:14:09.797",
    "msgType":"DATA"
}
EOF

# 2. 用wget发送文件内容
wget --header="Content-Type: application/json" \
     --post-file=dashboard_payload.json \
     https://your-dashboard-endpoint-url.com/push
额外调试技巧
  • 如果想查看服务器的响应内容,加上-qO-参数让wget安静运行并输出响应:
    wget -qO- --header="Content-Type: application/json" \
         --post-data='{...}' \
         https://your-dashboard-endpoint-url.com/push
    
  • 若变量$message或$scriptname包含双引号、反斜杠等特殊字符,需要先转义:
    # 对变量进行JSON转义
    message_escaped=$(echo "$message" | sed 's/"/\\"/g; s/\\/\\\\/g')
    scriptname_escaped=$(echo "$scriptname" | sed 's/"/\\"/g; s/\\/\\\\/g')
    
    之后用转义后的变量替换原变量即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 20:37:45