简体   繁体   中英

Variable insertion in a json sent by curl to the Discord api. "unmatched close brace/bracket in URL position X"

I have a small problem when sending a curl request to Discord.

my script:

#!/bin/bash
discord_webhook="https://discord.com/api/webhooks/ID/XXXXX"
tmp_file=/tmp/toblock.tmp
blocked_ip=$(paste -s -d ' ' $tmp_file)
if ! [ -z "$discord_webhook" ]; then
curl -H "Content-Type: application/json" -d '{"username": "IP blocking", "embeds":[{"title":"New IP(s) blocked","description":"'$blocked_ip'"}]}' "$discord_webhook"
fi

content of /tmp/toblock.tmp is the two following lines (testing random ip):

15.15.15.15
16.16.16.16

the error is

root@vm1 ~ # bash ad.sh
curl: (3) unmatched close brace/bracket in URL position 13:
16.16.16.16"}]}
            ^

Would someone see where the error is in my Curl request?

$blocked_ip contains spaces, so that's splitting up the JSON into multiple arguments. You need to put it in double quotes to prevent this.

curl -H "Content-Type: application/json" -d '{"username": "IP blocking", "embeds":[{"title":"New IP(s) blocked","description":"'"$blocked_ip"'"}]}' "$discord_webhook"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM