What fields are required when POSTing a worklog to JIRA?
Asked Answered
V

2

5

I'd like to populate my JIRA database programmatically, by POSTing CSV data via the API. I have the URL and authentication working, but I consistently get 400 errors: "Bad Request". My guess is that I'm missing some required field in the payload:

{
"comment": "test",
"self": "https://jira.mycompany.com/rest/api/latest/issue/12345",
"started": "2015-12-09T17:29:14.698-0500",
"timeSpent": "1 s",
"timeSpentSeconds": "1"
}

The documentation has an example, but it includes some fields which appear to be inapplicable to a POST, such as the worklog ID - which surely must be created by the server, not the client. The query parameters are all optional.

One user claims to have success with just three fields: comment, started, and timeSpent. I still get a 400 with just those fields. Another did the same, after adjusting the time format; I'm matching his or her format. Other users can POST using comment, started, and timeSpentSeconds.

A common problem has been setting content-type appropriately; I believe I have that covered, as I get a 415 "Unsupported Media Type" if I use a type other than "application/json".

I hear that there's a specific product required to use the API for writing (as opposed to reading, which works fine). If we were missing that license, I would expect to get an error message to that effect, however.

Here's the code, with a little verbosity for debugging:

// Initialize various parameters
string  url        = baseUrl + "issue/" + issueKey + "/worklog";
var     rawContent = // Double-quote and concatenate parameters, and wrap them in curly brackets
var     client     = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", encodedCreds);
var     content    = new StringContent(rawContent, Encoding.UTF8, "application/json");

// Using POST gives a 400 Bad Request error; are we missing a required field?
var  task     = client.PostAsync(url, content);  task.Wait();
var  response = task.Result;

Am I missing a required field? There's no change in the error returned if I POST with no content, or with invalid field names. It would be nice if JIRA would deign to tell me what fields it is expecting but not getting, or getting but not expecting.

Very similar, but focused on 500 errors.

Vaginismus answered 10/12, 2015 at 0:5 Comment(2)
Where's the value assignment for rawContent or have you purposefully removed it?Astrobiology
It's removed; see the comment where the assignment would be. Assigning the JSON data to rawContent is non-trivial, and I didn't want to clutter the code example. It generates the text shown before the code, in the top half of the question.Vaginismus
V
8

The problem was not one of fields: comment, started, and timeSpentSeconds are in fact enough. Rather, the problem was that I was testing with just one second of time. Apparently, JIRA expects work to be logged in units of at least one minute, even though it explicitly tracks timeSpentSeconds!

This is the exact error message one will get when trying to upload with (e.g.) "timeSpentSeconds": 30":

"errorMessages" : ["Worklog must not be null."],
"errors" :
    {
    "timeLogged" : "You must indicate the time spent working."
    }

However, I was able to successfully upload work with this body:

{ 
"comment": "test",
"started": "2015-12-10T13:45:01.778-0500",
"timeSpentSeconds": "60"
}

For future visitors: I eventually abandoned this approach as writing to the work log always records under your own account. An admin cannot use the API to record work for other people, only for him- or herself.

Vaginismus answered 10/12, 2015 at 19:54 Comment(0)
T
0

I think the started key do not necessary for the body request and it will that time you request to create the worklog. This below code will raise error with the same of above answer.

{
    "comment": {
        "content": [
            {
                "content": [
                    {
                        "text": "Testing Comment",
                        "type": "text"
                    }
                ],
                "type": "paragraph"
            }
        ],
        "type": "doc",
        "version": 1
    },
    "timeSpentSeconds": 59
}

For this case, I remove the started key from the body request and it was worked when timeSpentSeconds gather than 60 seconds.

So, you should add condition to your code for adjust this value if it less than 60 seconds.

Tabulate answered 22/4 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.