How can I fetch the value of a label of an issue via a curl command with jql written in the url?

Ryan Kaviani May 27, 2021

I am a complete beginner in terms of knowledge of JIRA and JQL, so I apologize if I ask 'the same thing' twice. Please do your best to explain things at a beginner level.

I understand curl on linux transfers server data, and in my case it just prints onto a terminal. I want to be able to run a curl command on a JIRA issue page, and have it print out a specific value--the value of the labels.

I have read that JQL does not support this since JQL cannot return field values specifically, but I wanted to ask this question myself to see if there was an update. Can I use JQL to return a field value ONLY? That way when I run my curl command I print that value ONLY?

Thank you ahead of time, I do not expect a 'yes,' I would just like to know if I am wasting my time looking for an answer.

1 answer

1 accepted

2 votes
Answer accepted
Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 27, 2021

You can utilize the API to send a JQL query and specify the the field(s) you would like returned. The response will contain a bit more than just the label values, but not much more.

Sample URL you can call from curl (change YOURDOMAIN to your Jira app's domain, e.g. jira.mycompany.com the the issue ID to your issue of interest):
https://<YOURDOMAIN>/rest/api/2/search?jql=id=BR-12411&fields=labels

Sample response:
{"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues":[{"expand":"operations,versionedRepresentations,editmeta,changelog,renderedFields","id":"67810","self":"https://YOURDOMAIN/rest/api/2/issue/67810","key":"BR-12411","fields":{"labels":["devops"]}}]}

Ryan Kaviani May 27, 2021

I see now, I was hitting the wrong endpoint. I've built a command now that is:

curl --request GET --url 'https://<MYDOMAIN>/rest/api/2/field' --header 'Accept: application/json'

and it just returns everything (still a step in the right direction). Now at this point I turn to your URL sample.

At this point I understood your code, so I tried it, but for some reason when I tried the command you referenced, I was getting a total of 0 hits. I know for a fact that I have the right ID, because I manually used it in the search bar and it returned the right issue, but it won't print anything on the terminal. I just get this back:

{"startAt":0,"maxResults":50,"total":0,"issues":[]}

Is there something I am missing? This is the command that is returning no hits, despite there really being one:

curl --request GET --url 'https://<MYDOMAIN>/rest/api/2/search?jql=id=XXXXXXX-000' --header 'Accept: application/json'

Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 27, 2021

You're likely not authenticated; try the following. Note also that I have to use double quotes rather than single quotes with curl.

curl --user USERNAME --request GET --url "https://YOURDOMAIN/rest/api/2/search?jql=id=BR-12411&fields=labels"

or

curl --user USERNAME:PASSWORD --request GET --url "https://YOURDOMAIN/rest/api/2/search?jql=id=BR-12411&fields=labels"

Ryan Kaviani May 27, 2021

Thanks for the help! I finally got what you did! I have a couple more questions if you could still help:

1) The place where you input a password is called the api_token according to the REST API, is this the same as an OAuth token that you can generate? I tried using one I just made but it wouldn't authenticate me. If not, how can I generate one so that I wouldn't need to enter my password in the code or enter it every time I call this command?

2) Say I wanted to search by project name instead ,but I have a space in a project title (i.e.- "Example Title"). How would I write this?

I'm trying to do: .../search?jql=project="Example Title"&fields=labels

Like Payne likes this
Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 27, 2021

1. Hmm, that's beyond my level of experience and understanding. Perhaps someone else can chime in with some guidance on that.

2. Use %20 in place of spaces, and wrap the project name in single quotes, e.g.

curl --user USERNAME --request GET --url "https://YOURDOMAIN/rest/api/2/search?jql=id=BR-12411%20and%20project='Business%20Operational%20Requests'&fields=labels"

or, you can also use the project key (BR in my case) rather than the name.

Like Ryan Kaviani likes this

Suggest an answer

Log in or Sign up to answer