Search code examples
c#postjira-rest-api

What fields are required when POSTing a worklog to JIRA?


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.


Solution

  • 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.