为什么我的JSON请求主体显示为无效,而之前类似的请求正在工作?

hi3rlvi2  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(103)

我有以下代码片段,我试图将通知数据发送到discord webhook,但它返回{"code": 50109, "message": "The request body contains invalid JSON."}%
以下是非工作代码(JSON请求部分加粗):

if [ "$DISCORD_WEBHOOK_URL" != "" ]; then
    rclone_sani_command="$(echo $rclone_command | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g')" # Remove all escape sequences
    # Notifications assume following rclone ouput:
    # Transferred: 0 / 0 Bytes, -, 0 Bytes/s, ETA - Errors: 0 Checks: 0 / 0, - Transferred: 0 / 0, - Elapsed time: 0.0s
    transferred_amount=${rclone_sani_command#*Transferred: }
    transferred_amount=${transferred_amount%% /*}
**    send_notification() {
      output_transferred_main=${rclone_sani_command#*Transferred: }
      output_transferred_main=${output_transferred_main% Errors*}
      output_errors=${rclone_sani_command#*Errors: }
      output_errors=${output_errors% Checks*}
      output_checks=${rclone_sani_command#*Checks: }
      output_checks=${output_checks% Transferred*}
      output_transferred=${rclone_sani_command##*Transferred: }
      output_transferred=${output_transferred% Elapsed*}
      output_elapsed=${rclone_sani_command##*Elapsed time: }
      notification_data='{
            "username": "'"$DISCORD_NAME_OVERRIDE"'",
            "avatar_url": "'"$DISCORD_ICON_OVERRIDE"'",
            "content": null,
            "embeds": [
              {
                "title": "Rclone Local Backup: Finished!",
                "color": 65408,
                "fields": [
                  {
                    "name": "Directories synced",
                    "value": "'"$SOURCE_DIR"' to '"$DESTINATION_DIR"'"
                  },
                  {
                    "name": "Transferred",
                    "value": "'"$output_transferred_main"'"
                  },
                  {
                    "name": "Errors",
                    "value": "'"$output_errors"'"
                  },
                  {
                    "name": "Checks",
                    "value": "'"$output_checks"'"
                  },
                  {
                    "name": "Transferred",
                    "value": "'"$output_transferred"'"
                  },
                  {
                    "name": "Elapsed time",
                    "value": "'"$output_elapsed"'"
                  },
                  {
                    "name": "Finished",
                    "value": "Finished backup on '"$END_DATE"' at '"$END_TIME"'"
                  },
                ],
                "thumbnail": {
                  "url": null
                }
              }
            ]
          }'
          curl -H "Content-Type: application/json" -d "$notification_data" $DISCORD_WEBHOOK_URL
    }**
    if [ "$transferred_amount" != "0" ]; then
      send_notification
    fi
  fi
  rm -f "$LOCK_FILE"
  trap - SIGINT SIGTERM
  exit
fi

在同一脚本的前面,以下代码片段正确地向discord发送了通知:

touch "$LOCK_FILE"
  echo "Starting to backup $SOURCE_DIR to $DESTINATION_DIR on $START_DATE at $START_TIME" | tee -a $LOG_FILE
  **send_notification_start() {
    notification_data='{
          "username": "'"$DISCORD_NAME_OVERRIDE"'",
          "avatar_url": "'"$DISCORD_ICON_OVERRIDE"'",
          "content": null,
          "embeds": [
            {
              "title": "Rclone Local Backup: Started!",
              "color": 4094126,
              "fields": [
                {
                  "name": "Started",
                  "value": "Started backup on '$START_DATE' at '$START_TIME'"
                },
                {
                  "name": "Directories being synced",
                  "value": "'$SOURCE_DIR' to '$DESTINATION_DIR'"
                }
              ],
              "thumbnail": {
                "url": null
              }
            }
          ]
        }'
        curl -H "Content-Type: application/json" -d "$notification_data" $DISCORD_WEBHOOK_URL
  }**

我试着修改引号和其他字符,看看是否是这个问题,但我无法弄清楚为什么这个JSON请求体是不正确的。
通过JSON验证器运行这两个JSON代码片段中的任何一个(粗体),我只是得到它在开始时就错误地解析了(即使后者工作正常)。
我是一个使用JSON的新手,但我想在我的脚本完成备份时,收到一个发送到discord的通知,其中包含一些具体发生的细节。

k97glaaz

k97glaaz1#

最后一个JSON括号:

{
                "name": "Finished",
                "value": "Finished backup on '"$END_DATE"' at '"$END_TIME"'"
              },

有逗号。
最后的}后面不应该有逗号,所以应该是:

{
                "name": "Finished",
                "value": "Finished backup on '"$END_DATE"' at '"$END_TIME"'"
              }

希望能帮上忙。

相关问题