如何解决Unix服务器通过wget发送JSON至仪表板的解析错误
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

