【问题标题】:What fields are required when POSTing a worklog to JIRA?将工作日志发布到 JIRA 时需要哪些字段?
【发布时间】:2016-03-15 11:16:59
【问题描述】:

我想通过 API 发布 CSV 数据,以编程方式填充我的 JIRA 数据库。我有 URL 和身份验证工作,但我一直收到 400 错误:“错误请求”。我的猜测是我在有效负载中缺少一些必填字段:

{
"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,但它包含一些似乎不适用于 POST 的字段,例如工作日志 ID - 它肯定必须由服务器而不是客户端创建。 query 参数都是可选的。

One user claims 只需三个字段即可获得成功:commentstartedtimeSpent。仅这些字段我仍然得到 400。 Another 做了同样的事情,在调整了时间格式之后;我正在匹配他或她的格式。 Other users 可以使用 commentstartedtimeSpentSeconds 进行 POST。

一个常见的问题是适当地设置内容类型;我相信我已经涵盖了这一点,因为如果我使用“application/json”以外的类型,我会得到 415“不支持的媒体类型”。

我听说使用 API 进行写作需要特定的产品(而不是阅读,它工作正常)。但是,如果我们缺少该许可证,我希望会收到一条错误消息。

代码如下,调试时略显冗长:

// 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;

我是否缺少必填字段?如果我 POST 没有内容或字段名称无效,则返回的错误没有变化。如果 JIRA 愿意告诉我它期待但未获得的字段,或者获得但未期待的字段,那就太好了。

Very similar, but focused on 500 errors.

【问题讨论】:

  • rawContent 的赋值在哪里,还是你故意删除了它?
  • 已移除;请参阅任务所在的评论。将 JSON 数据分配给 rawContent 并非易事,我不想弄乱代码示例。它生成代码前显示的文本,在问题的上半部分。

标签: c# post jira-rest-api


【解决方案1】:

问题不在于字段之一:commentstartedtimeSpentSeconds 实际上就足够了。相反,问题在于我只用了一秒钟的时间进行测试。显然,JIRA 希望以至少一分钟为单位记录工作,即使它明确跟踪 timeSpentSeconds!

这是尝试使用(例如)"timeSpentSeconds": 30" 上传时会收到的确切错误消息:

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

但是,我能够成功上传使用此正文的作品:

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

对于未来的访问者:我最终放弃了这种方法,因为写入工作日志总是记录在您自己的帐户下。管理员不能使用 API 为其他人记录工作,只能为他或她自己。

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多