Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass a bash script array to Datacenter Jira Rest API when creating an issue

Ahmed Nadeem
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 30, 2024

I have a bash script where I am storing some hostnames in a bash array which I read in from a file. I would then like to pass that array to the description on the jira I am trying to create using the Jira Server Rest API v2. However, with the following code, I keep getting an error. If someone can provide some guidance it'd be much appreciated.

 

 

counter=0
declare -a hosts
while read line
do
hosts[$counter]=$line
done < hostsfile.txt

curl --request POST --url 'https://myjiraurl.com/rest/api/2/issue' --header 'Authorization: Basic <auth_code>' --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{ "fields": { "description": "This is an automated JIRA.\nHost List:'${hosts[@]}'", "issuetype": { "id": "10000" }, "project": { "id": "13806" }, "summary": "Host List" } }'

 

The error I am getting is

 

{"errorMessages":["Unexpected end-of-input in VALUE_STRING\n at [Source: org.apache.catalina.connector.CoyoteInputStream@6bc37815; line: 1, column: 239]"]}curl: (7) Failed to connect to host1 port 80: Connection refused
curl: (7) Failed to connect to host2 port 80: Connection refused
curl: (7) Failed to connect to host3 port 80: Connection refused
curl: (3) [globbing] unmatched close brace/bracket in column 106

 

 

1 answer

1 accepted

0 votes
Answer accepted
Jim Knepley - ReleaseTEAM
Atlassian Partner
August 30, 2024

You've bumped up against bash quoting rules, which can be challenging. Regardless, I prefer to use jq to construct JSON data in scripts.

For example:

HOSTS=$(tr "\n" " " < hostsfile.txt | sed 's/ $//')
DATA_JSON=$(jq --null-input --compact-output --arg hostlist "${HOSTS}" \
'{"fields":{"description":("Host List: "+$hostlist),"issuetype":{"id":"10000"},"project":{"id":"13806"},"summary":"Host List"}}')

 

Ahmed Nadeem
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 3, 2024

This worked, thanks much!

Suggest an answer

Log in or Sign up to answer