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

如何解决Unix服务器通过wget发送JSON至仪表板的解析错误

Fixing "Unexpected char 10" JSON Parsing Error in Shell Script POST Request

That error message points directly to an unescaped newline character (ASCII 10) in your JSON payload. Here's what's happening and how to fix it:

Why This Happens

Your generate_post_data function outputs multi-line JSON, and when wget sends this via --post-data, those raw newline characters get included in the request. JSON parsers don't tolerate unescaped newlines outside of string values, hence the parsing failure.

Solution 1: Make the JSON a Single Line

Rewrite your generate_post_data function to output a single line of valid JSON, removing all line breaks:

generate_post_data() {
cat <<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
}

Solution 2: Strip Newlines Before Sending

If you prefer keeping the multi-line format for readability, pipe the output through tr to delete newlines before passing it to wget:

/usr/bin/wget --no-check-certificate --header="Accept: application/json" --header="Content-Type:application/json" --post-data "$(generate_post_data | tr -d '\n')" "https://cas.internal.unicreditgroup.eu:8181/Aurora/rs/nores/renosub"

Bonus: Avoid Manual JSON Escaping with jq

Manually escaping nested JSON (like your data field) is error-prone. If jq is installed on your server, use it to generate valid JSON automatically:

generate_post_data() {
  jq -n \
    --arg action "Action to do for test nr: 1" \
    --arg critical "LOW" \
    --arg fixstatus "NOTCONCERN" \
    --arg host "MTR_SOME_HOST" \
    --arg message "$message" \
    --arg app "MTR" \
    --arg mxtype "PROD" \
    --arg script "$scriptname" \
    --arg msg "NotificationReceiveDTO without dict to send at: 2020-06-07T11:14:09.794 Created at: 2020-06-07T11:14:09.797" \
    '{
      data: ($ARGS.named | del(.msg) | tojson),
      msg: $msg,
      msgType: "DATA"
    }'
}

This approach handles all escaping automatically, even if your variables contain special characters like quotes or newlines.

After applying any of these fixes, re-run your script and check the dashboard logs— the parsing error should be resolved.

内容的提问来源于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 21:52:34