Forums

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

Null result for GET REST API Jira Cloud

Constance Hua (Private) August 10, 2018

I am trying to search user by e-mail address. I am using:

def userResult = get("/rest/api/2/user/search?username=hello@gmail.com")
.header('Content-Type', 'application/json')
.asObject(Map)

if (userResult.status == 200) {
return userResult.body.fields
}
else {
return "result.status"
}

I get this response:

"Cannot get property 'fields' on null object"

I tried with each user, email and username and nothing changes.

 

Thank you for your help

2 answers

1 accepted

1 vote
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
August 10, 2018

Hi Constance,

Thank you for your question.

Further to my previous reply I believe that if you made the following changes below to your script then it should work.

  • Change the line of '.asObject(Map)' to be '.asObject(List)'
  • Change the line of 'return userResult.body.fields' to be 'return userResult.body'

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Constance Hua (Private) August 10, 2018

Thank you Kristian for your help!

Do you think you can help me get only the "name" element instead of the entire body? :)

Kristian Walker _Adaptavist_
Community Champion
August 10, 2018

You can just change the return line to be 'return userResult.body.name' to get just the name element out of the body.

Constance Hua (Private) August 10, 2018

Amazing thank you!

0 votes
Kristian Walker _Adaptavist_
Community Champion
August 10, 2018 edited

Hi Constance,

Thank you for your question.

I can confirm that to search for a user you need to use the Get user rest API described here rather than the search api.

I have created an example script located here which can be ran from the script console to return a user based on a specified username and you can modify this example to  suit your needs.

Please note the Get user API does not permit searching on an email address and as stated in the documentation only permits searching for a user using the username or user key. 

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer