Forums

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

How to handle exceptions for invalid user name if assigned via jira rest API

Shrinish August 21, 2019

Hello,

 

I am trying to set the assignee of the user as per the command:

PUT: https://localhost:8080/rest/api/2/issue/issue-key/assignee
Content-Type: application/json
Auth: Basic Auth

 

I am getting the following error: 

{ "errorMessages": [], "errors": { "assignee": "User 'sdxwwr' does not exist." }}

 

Could someone help me with the code to catch this exception

 

Note: The field where the user enters his name is a text field

1 answer

1 accepted

0 votes
Answer accepted
DPKJ
Community Champion
August 21, 2019

Your rest call should look like this,

curl -D- -u username:password -X PUT -H "Content-Type: application/json" https://<JIRA_BASE_URL>/rest/api/2/issue/<ISSUE_KEY>/assignee -d "{\"name\":\"<ASSIGNEE_NAME>\"}"

 

Once you hit this API get response and parse it as JSON and it contains key 'errorMessages' or 'errors', it implies you had some error setting assignee. In your case above 'sdxwwr' is not user in your Jira.

 

If you want to know how to do it in any programming language let me know.

Shrinish August 21, 2019

Hi DPK J,

 

Yes I would like to know how to do it in C#.

And what does this mean ?

-d "{\"name\":\"<ASSIGNEE_NAME>\"}"
DPKJ
Community Champion
August 21, 2019

This is JSON payload that you send to API as body just like you do when you call POST methods.

Shrinish August 21, 2019

{
"name":"sdxwwr"
}

 

Yep that is how I do. Could you tell me some way to do it in C#

Shrinish August 21, 2019

I mean the handling of exception:

{ "errorMessages": [], "errors": { "assignee": "User 'sdxwwr' does not exist." }}

DPKJ
Community Champion
August 21, 2019

I cannot provide you with C# sample as I am on Mac but here is Node.JS sample that can help you.

// This code sample uses the 'request' library:
// https://www.npmjs.com/package/request
var request = require('request');

var bodyData = `{
  "name": "<ASSIGNEE_NAME>"
}`;

var JIRA_BASE_URL = "<YOUR_BASE_URL>";
var ISSUE_KEY = "<ISSUE_KEY_OR_ID>"; var options = { method: 'PUT', url: `${JIRA_BASE_URL}/rest/api/2/issue/${ISSUE_KEY}/assignee`, headers: { 'Authorization': "BASE64_ENCODED<USERNAME>:<PASSWORD>", 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: bodyData }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log( 'Response: ' + response.statusCode + ' ' + response.statusMessage ); console.log(body); });
Shrinish August 21, 2019

Thanks I will try to get reference. Cheers !

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events