-1
status=$(curl -i -s -H "Authorization: token abc123" https://www.some_url.com | awk 'NR==18')

if [ "$status" != 200 ]; then
  echo "$status"
fi

The above code is working and echo is printing the output in my console.

But when I use discord webhook, it doesn't send message there and throws following error: {"code": 50109, "message": "The request body contains invalid JSON."}

webhook=https://discord.com/api/webhooks/abc123

status=$(curl -i -s -H "Authorization: token abc123" https://www.some_url.com | awk 'NR==18')

if [ "$status" != 200 ]; then
  curl -H "Content-Type: application/json" -X POST -d '{"content":"'"$status"'"}' $webhook
fi

I can send a normal message through webhook but not this curl output. The output is suppose to be HTML error message.

Even if I change awk 'NR==18' to awk 'NR==1', it's not sending even the one line output which is suppose to be something like: HTTP/1.1 401 Unauthorized

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Learner
  • 51
  • 8

1 Answers1

1

I have a discord bot (webhook) running on a raspberry pi from a bash script, so I think it is the same thing you are looking for

sendcontent='{"username": "Botname", "content": "'"${messagearray[@]}"'"}'
curl -H "Content-Type: application/json" -d "$sendcontent" "$webhookurl"

I would suggest the following change to your code:

messagearray=($(curl -i -s -H "Authorization: token abc123" https://www.some_url.com | awk 'NR==18'))

EDIT: It seems that there are nasty invisible characters in the curl+awk command return, therefore the above solution needs to be modified as below (This bash file test with the provided url from comments)

#!/bin/bash
webhookurl="https://mywebhook"
messagearray=($(curl -i -s https://www.google.com/ | awk 'NR==1'))
#this next line removes nasty characters that give json errors (keeps only letters, numbers SPACE and -)
message=`echo ${messagearray[@]} | sed 's/[^0-9A-Za-z -]*//g'`;
sendcontent='{"username": "Botname", "content": "'"$message"'"}'
curl -H "Content-Type: application/json" -d "$sendcontent" "$webhookurl"

This successfully sends HTTP/2 200 in discord

the removal is an adaptation of: How to remove a newline from a string in Bash

Gerge
  • 93
  • 6
  • Thanks @Gerge I'm finally able to send message to discord but only half of it. Like instead of **HTTP/1.1 401 Unauthorized**, it's sending only **HTTP/1.1**. Same for next lines. Instead of **Server: nginx**, only sending **Server:** and so on. Basically it's not sending the text after space. Any solution for that? – Learner Jan 29 '22 at 20:56
  • 1
    Notice the extra () brackets I suggested to turn it into an array. an array is a set of words separated by spaces and to print all of them you need to use the `${messagearray[@]}` the @ tells it to print all. From your comment I guess you are simply using `$messagearray` like it would be a string, then that results in what you describe only the first part printed – Gerge Jan 31 '22 at 21:29
  • 1
    It would help if you specify the content of ${messagearray[@]} or the output you are trying to pass through, so I can test with it. – Gerge Jan 31 '22 at 21:42
  • I did change it to `${messagearray[@]}` but then it shows the error `curl: (7) Failed to connect to 200 port 80: Connection refused` and `curl: (3) unmatched close brace/bracket in URL position 3: "}`. You could test it for this line: `messagearray=($(curl -i -s https://www.google.com/ | awk 'NR==1'))` which should send the message **HTTP/2 200** to discord. – Learner Feb 01 '22 at 20:59
  • 1
    It seems I solved it (there might be other nasty characters). I will edit the answer in a minute – Gerge Feb 04 '22 at 22:00
  • 1
    This was a fun hour spent chasing literally nothing because I could not see it. If this solved your problem please mark the answer as correct. – Gerge Feb 04 '22 at 22:17
  • Thanks a lot. This helps. But I'm getting `{"code": 50109, "message": "The request body contains invalid JSON."}` error if I try `NR==12` or any line after like NR==16 or 17... Could you help me with that as well? – Learner Feb 05 '22 at 13:26
  • 1
    OK, at this point is is easier to specify to only keep letters and numbers in the message. I edited the answer again, this time it removes everything that is not readable text. – Gerge Feb 10 '22 at 10:50