Redmine API 声称缺少 'hours' 字段和 returns 422 POST 到 time_entry.json

Redmine API claims missing 'hours' field and returns 422 on POST to time_entry.json

我正在尝试在客户端软件中创建 time entries in a Redmine 3.3.0.stable installation via the REST API, using the Requests library。我正在使用以下代码:

    urlSuffix='/time_entries.json'

    time_entry = {
        'hours':       0.25,
        'project_id':  1,
        'comments':    'test',
        'activity_id': 1,
    }

    params = {
        'key': session.APIKey,
    }

    headers = {
        'Content-Type': 'application/xml',
    }

    requestURL = "%s%s" % (session.redmineBaseUrl, urlSuffix)
    response = requests.post(requestURL, json=time_entry, params=params)

response.status_code 始终是 422,并且响应包含以下本地化消息:

{"errors":["Stunden muss ausgefüllt werden"]}

转换为

hours must be provided

但是,如您所见,字段 'hours' 的输入是正确的。此外,日志文件表明参数正确到达Redmine:

Started POST "/PROJECTNAMEHERE/time_entries.json?key=APIKEYHERE" for 62.178.175.81 at 2016-11-15 00:19:47 +0100
Processing by TimelogController#create as JSON
  Parameters: {"project_id"=>"1", "spent_on"=>"2016-11-15T00:19:24.513001", "comments"=>"lkj", "activity_id"=>"1", "hours"=>0.25, "key"=>"a009fdb7d15f174b31860cfa9d56fd648737d862"}
  Current user: MichaelJaros (id=5)
  Rendered common/error_messages.api.rsb (0.2ms)
Completed 422 Unprocessable Entity in 22ms (Views: 0.7ms | ActiveRecord: 5.1ms)

我试过同样的结果:

我最后的选择是尝试其中一种 Python Redmine libraries,但我看不出在这种情况下他们应该做些什么不同。

time_entry (required): a hash of the time entry attributes, including: [...]

time entries 文档中?当请求采用 XML 格式时,我希望 API 也需要 XML 数据用于 time_entry,而不是 "a hash"。但也许那部分只是写得不准确?

我做了 pip install python-redmine 并且下面的代码完美运行:

from redmine import Redmine
redmine = Redmine (redmineBaseUrl, version='3.3.0', key=APIKey)
[...]
redmine.time_entry.create(project_id=self.projectId, activity_id=self.activityId, hours=self.getHoursRounded(), spent_on=self.startDateTime.date(), comments=self.comment)

我应该在昨天搞砸几个小时之前这样做。

有同样的问题。用 time_entry 标签包装时间条目解决了我的问题。

发件人:

   time_entry = {
        'hours':       0.25,
        'project_id':  1,
        'comments':    'test',
        'activity_id': 1,
    }

收件人:

   time_entry = {
        'time_entry': {
             'hours':       0.25,
             'project_id':  1,
             'comments':    'test',
             'activity_id': 1,
        }
    }