Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to update an existing Jira Ticket with rest API in C#?

In the rest API Documentation (https://developer.atlassian.com/server/jira/platform/jira-rest-api-example-edit-issues-6291632/)

I found something like this command:

curl -D- -u fred:fred -X PUT --data "json_data" -H "Content-Type: application/json" http://kelpie9:8081/rest/api/2/issue/QA-31

I have very little idea from curl and http requests, so I used a converter on https://curl.olsh.me (see picture below)

test.PNG

 

I of course replaced the link, fred:fred with my username and password+token and replaced "json_string" with actuall fields.
It still does not work. I get response 200:OK but the ticket doesn't change.

Also why does the converter tell me that parameter -D- is not supported? Is it because of this, that it doesn't work?

What am I missing?

5 answers

Hi @CremeFresh ,

would you please share your request body (provided json_string) with us? In general I can't see anything wrong with your request...

The PUT request sort of worked, hence the 200 response, but you didn't tell JIRA what to update because you didn't supply the array of fields to update. Your request essentially says 'Update ticket QA-31' and nothing else.

If the PUT request is properly formed and executed, you should receive a response with a status of "204 No Content".

With regards to that tool reporting that '-D-' is not supported, I think there's a typo on that Jira documentation page, as curl command options start with a single hyphen for the abbreviation or double hyphens for the long version; there is no following hyphen, unless that is an obscure flavour of curl.

'-D' is the abbreviation for '--dump-header' option, so the curl command would be:

curl -D -u fred:fred -X PUT --data {see below} -H "Content-Type: application/json" http://kelpie9:8081/rest/api/2/issue/QA-31

I tried that with the tool, but it still didn't accept it, so I think it's just a limitation of the tool. Dumping the header is of no great benefit, so you can omit that option just fine and the tool will process and translate it to C# without the error message.

So, as an example, the following curl command to assign an issue to the user Harry (the first example on the Jira documentation page), using fully expanded options, would be:

curl --user fred:fred --request PUT --data '{"fields": {"assignee":{"name":"harry"}}}' --header 'Content-Type: application/json' --header 'Accept: application/json' --url http://kelpie9:8081/rest/api/2/issue/QA-31

 

Hello, thank you both for taking time by replying @Hana Kučerová  and @David Bakkers .

To be more concrete now, this is how it looks like:
This is my actual code:

Unbenannt1.PNG

And my json-string  which is in data looks like this:

Unbenannt.PNG

I am 100% sure that these version names and values exist, because I can create the tickets with these values. But changing the fields doesn't work. It keeps on staying the old values/names for the fields above.

I can provide you the exact response when I am at work.

What other testing have you done to isolate the cause of the problem?

1. Have you confirmed that the C# code is correct? IE, can you use that code to update just the value of customfield_14702, or to set some other Jira setting like the assignee?

2. Have you confirmed the JSON data of the request is valid? IE, can you can use that same JSON data in a REST API test tool like Postman to set the field values?

3. Have you tried putting the JSON data directly into the code not a variable? IE, does the request work if don't have it stored as a variable, but as a string typed directly in the code?

In your variable 'data' you have started with an outer array called '[JSON]' ... what is the purpose of that?

It's not clear whether you're asking for someone to help you debug possibly faulty C# code, a faulty REST API request, or a faulty request construction method.

@David Bakkers 

1. I cannot update a single field. Also tried it with other fields and simpler data strings.

2. Yes, I use this form of json also when creating tickets and it works

3. No, because of 2., but I will still try 

The variable data doesn't have another array [JSON]. In Debug-Mode I can chose how to view the data e.g. as string, as HTML or as JSON. The string form starts with something like {\fields\: etc...


I am sorry if its not clear what I am asking for, because I really don't know. I wish I just could tell you that its faulty c# code, but everything seems to work fine, because I also get the 200 response. 

One weird think I had though: Even though I used totally jibberish username:password, I still got the 200 response: OK and also no crashes. How can that be?

Hmm. If your C# code above works to create a ticket using a POST request, then it should work for doing a PUT request. As to why you get no error with an invalid username / password... that is very strange.

Sorry, but my C# isn't good enough to spot any specific faults in the code. You might have to Google some other C# examples, as all the examples on the Jira Cloud REST API reference page are for other languages.

Personally, I use Postman to pre-test any tricky requests to the REST API to make sure my JSON data and the request is properly formed, as it is very interactive with error logging and reporting, plus it allows you to abstract your request from the code you're using to submit it.

Sorry I can't help much further.

Hana Kučerová Community Leader Feb 01, 2021

Hi @CremeFresh ,

it seems to me, you are on the server, even though there are cloud labels, so I used this documentation.

What is really strange - the return code 200. This should not be possible, even if you send empty body. So I suspect, that the PUT method is not set correctly and instead of it GET is used (or something like this).

I'm not C# developer, but I googled this a little bit and usually the request in the scripts I found has been defined like this:

var request = new HttpRequestMessage(HttpMethod.Put, link)

Would you please try it?

If you want to update Fix Version/s field, the request body should look like this:

{
"fields": {
"fixVersions": [{"name": "1.0"}]
}
}

So I think "versions" should be replaced by "fixVersions".

Which type does your customfield_14702 have? Is it select list with single choice or text field or something else?

Thank you.

Thank you very much, you might be on to something with the Put Method.
I will try it tomorrow when I am at work. 

The customfield_14702 is single choice from a list. I think thats not the issue though, as it works like this when I create tickets.

versions and fixVersions are two different fields actually and they both exist. Similar to the customfield_14702 I created a ticket with versions, so that also should be correct I think. Especially because versions is a mandetory field when creating tickets.


Thank you anyways,

I will try using Postman.

I just used Postman to try updating some fields in an existing issue by doing a PUT request to the same Jira Cloud REST API v2 endpoint you've tried and it worked fine, and I got the expected '204 No Content' response:

Postman PUT example 1.JPG

When I tried the same request with no JSON in the body of the request, I got the expected '400 Bad Request' response. When I tried the same request with faulty credentials, I got the expected '401 Unauthorized' response.

So, I've just noticed in your screenshot of your C# code, you've blanked out the part of the connection URL string for your Jira instance.... including if you've specified a port. You're not trying to make the connection on port 8081 are you? That's only a example shown on the Atlassian page for a possible custom configuration.

By default, the REST API works on port 443. the same as the web interface. If you haven't changed the default port, you should omit any port number in the URL connection string.

That could be it. My link looks like this:
https://cocoa.myfirmaname.de/sjira/rest/api/2/issue/{projectName}-{ticketID}

myfirmname is in this case of course a placeholder for the real name

 

When I try the link above with the correct projectname and ticketID , I open up the website as json format. So thats why I considered it to be the right link.

 

.

I just tested it with Postman and there I cannot even do a Get Request.
I think my authentification is somehow incorrect?

I choose  Basic Auth.
Our systems works like this: 
-Username
-Password+Token

I figured that this type of Auth is Basic

Could this be the reason it doesn't work?:

When I try to GET or PUT I get the main page as a response. And usually on that page you have to login with your username and password.

You will not have access to the issue link when you don't login that way first.

So my guess is, that the Authentification has nothing to do with the login process on the main page.

So another solution would be to first login, then use the cookies somehow to THEN send the PUT request.

This also would explain why I always get Response: 200 back, regardless of a wrong issueid and/or username and password, because I will always get as a response the login page.

I finally solved it by going a different route:
Just like when I create tickets, I used a WebDriver to login. Then I got the cookies and used that for a HttpRequest. I then stream the data to the server. I did the same just with the PUT method so to speak.

Glad to hear you found a method that works, but there are some other bizzare things about your authentication method that aren't explained.

1. If you are using Jira Server, not Jira Cloud, the REST API only recently acquired user tokens instead of passwords for authentication (read this article). When using the new tokens, you use them only, not password+token, so that means you're not using that version of Jira Server and the native user tokens feature.

2. This leads me to think you have some sort of third party tool like API Tokens for Jira, or something similar that generates web session cookies. You may want to check with your Jira admins and confirm exactly what authentication methods are enabled. If Basic Auth has been turned off, then consider re-coding your C# to use OAuth wholly; if not and you have the latest version of Jira, use Basic Auth with the native username / token instead.

Good luck

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you