Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

How to curl a post request with a bash array?

So I am trying to create a script that will send data to a slack hook app. This is my current code:

DATA="[app_code: app_code | error invalid _id: alsdjasdlkj];[app_code: app_code | error invalid _type: 1a1]"

IFS=';' ;read -ra errors <<< "$DATA"
unset IFS

cmd=$(curl -X POST -H 'Content-type: application/json' --data '{"text": "'"${errors[@]}"'" }' hook_url)

echo $cmd

The issue I am having is that I want to send an array list of errors as I will never know how many there could be at any given time. when I run this bash command this is the error I get:

curl: (3) bad range in URL position 2:
[app_code: app_code | error invalid _type: 1a1]" }

So how can I fix the code to send an array as a curl command for the data field? And if that's not possible is there a workaround for this? I need the text to show up on the slack channel as follows:

Total errors: 2
[app_code: app_code | error invalid _id: alsdjasdlkj]
[app_code: app_code | error invalid _type: 1a1]

Any help is appreciated. Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...