Project

General

Profile

Rest api » History » Version 93

Jean-Philippe Lang, 2014-12-22 14:58
JSON example for creating an issue with multiple attachments

1 26 Jean-Philippe Lang
{{>toc}}
2
3 1 Jean-Philippe Lang
h1. Redmine API
4
5 60 Jean-Philippe Lang
Redmine exposes some of its data through a REST API. This API provides access and basic CRUD operations (create, update, delete) for the resources described below. The API supports both "XML":http://en.wikipedia.org/wiki/Xml and "JSON":http://en.wikipedia.org/wiki/JSON formats.
6 1 Jean-Philippe Lang
7
h2. API Description
8
9 24 Jean-Philippe Lang
|_.Resource                     |_.Status     |_.Notes  |_.Availability|
10 91 Jean-Philippe Lang
|[[Rest_Issues|Issues]]         | Stable        |  | 1.0 |
11
|[[Rest_Projects|Projects]]     | Stable        |   | 1.0 |
12 55 Jean-Philippe Lang
|[[Rest_Memberships|Project Memberships]]  | Alpha | | 1.4 |
13 56 Jean-Philippe Lang
|[[Rest_Users|Users]]           | Stable | | 1.1 |
14
|[[Rest_TimeEntries|Time Entries]]           | Stable | | 1.1 |
15 28 Jean-Philippe Lang
|[[Rest_News|News]]             | Prototype | Prototype implementation for @index@ only | 1.1 |
16 43 Jean-Philippe Lang
|[[Rest_IssueRelations|Issue Relations]]  | Alpha | | 1.3 |
17
|[[Rest_Versions|Versions]]  | Alpha | | 1.3 |
18 85 Jean-Philippe Lang
|[[Rest_WikiPages|Wiki Pages]]  | Alpha | | 2.2 |
19 44 Jean-Philippe Lang
|[[Rest_Queries|Queries]]  | Alpha | | 1.3 |
20 63 Jean-Philippe Lang
|[[Rest_Attachments|Attachments]]  | Beta | Adding attachments via the API added in 1.4 | 1.3 |
21 53 Jean-Philippe Lang
|[[Rest_IssueStatuses|Issue Statuses]]  | Alpha | Provides the list of all statuses | 1.3 |
22 51 Jean-Philippe Lang
|[[Rest_Trackers|Trackers]]  | Alpha | Provides the list of all trackers | 1.3 |
23 84 Jean-Philippe Lang
|[[Rest_Enumerations|Enumerations]]  | Alpha | Provides the list of issue priorities and time tracking activities | 2.2 |
24 52 Jean-Philippe Lang
|[[Rest_IssueCategories|Issue Categories]]  | Alpha | | 1.3 |
25 55 Jean-Philippe Lang
|[[Rest_Roles|Roles]]  | Alpha | | 1.4 |
26 79 Jean-Philippe Lang
|[[Rest_Groups|Groups]]  | Alpha | | 2.1 |
27 92 Jean-Philippe Lang
|[[Rest_CustomFields|Custom Fields]]  | Alpha | | 2.4 |
28 24 Jean-Philippe Lang
29 15 Eric Davis
Status legend:
30 1 Jean-Philippe Lang
31
* Stable - feature complete, no major changes planned
32
* Beta - usable for integrations with some bugs or missing minor functionality
33
* Alpha - major functionality in place, needs feedback from API users and integrators
34
* Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration*
35
* Planned - planned in a future version, depending on developer availability
36
37 24 Jean-Philippe Lang
h2. General topics
38 1 Jean-Philippe Lang
39 78 Jean-Philippe Lang
h3. Specify @Content-Type@ on @POST@/@PUT@ requests
40 66 Etienne Massip
41 83 Jean-Philippe Lang
When creating or updating a remote element, the @Content-Type@ of the request *MUST* be specified even if the remote URL is suffixed accordingly (e.g. @POST ../issues.json@):
42 82 Jean-Philippe Lang
* for JSON content, it must be set to @Content-Type: application/json@.
43 78 Jean-Philippe Lang
* for XML content, to @Content-Type: application/xml@.
44 66 Etienne Massip
45 24 Jean-Philippe Lang
h3. Authentication
46
47
Most of the time, the API requires authentication. To enable the API-style authentication, you have to check *Enable REST API* in Administration -> Settings -> Authentication. Then, authentication can be done in 2 different ways:
48
* using your regular login/password via HTTP Basic authentication.
49 38 Jean-Philippe Lang
* using your API key which is a handy way to avoid putting a password in a script. The API key may be attached to each request in one of the following way:
50
** passed in as a "key" parameter
51
** passed in as a username with a random password via HTTP Basic authentication
52 46 John Galambos
** passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0)
53 38 Jean-Philippe Lang
54
You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout.
55 24 Jean-Philippe Lang
56 89 Jean-Philippe Lang
h3. User Impersonation
57
58
As of Redmine 2.2.0, you can impersonate user through the REST API by setting the @X-Redmine-Switch-User@ header of your API request. It must be set to a user login (eg. @X-Redmine-Switch-User: jsmith@). This only works when using the API with an administrator account, this header will be ignored when using the API with a regular user account.
59
60 90 Jean-Philippe Lang
If the login specified with the @X-Redmine-Switch-User@ header does not exist or is not active, you will receive a 412 error response.
61
62 24 Jean-Philippe Lang
h3. Collection resources and pagination
63
64 47 Tom Clegg
The response to a GET request on a collection ressources (eg. @/issues.xml@, @/users.xml@) generally won't return all the objects available in your database. Redmine version:1.1.0 introduces a common way to query such ressources using the following parameters:
65 24 Jean-Philippe Lang
66
* @offset@: the offset of the first object to retrieve
67
* @limit@: the number of items to be present in the response (default is 25, maximum is 100)
68
69
Examples:
70
71
<pre>
72
GET /issues.xml
73
=> returns the 25 first issues
74
75
GET /issues.xml?limit=100
76
=> returns the 100 first issues
77
78
GET /issues.xml?offset=30&limit=10
79
=> returns 10 issues from the 30th
80
</pre>
81
82
Responses to GET requests on collection ressources provide information about the total object count available in Redmine and the offset/limit used for the response. Examples:
83
84
<pre>
85
GET /issues.xml
86
87
<issues type="array" total_count="2595" limit="25" offset="0">
88
  ...
89
</issues>
90
</pre>
91
92
<pre>
93
GET /issues.json
94
95
{ "issues":[...], "total_count":2595, "limit":25, "offset":0 }
96
</pre>
97
98
Note: if you're using a REST client that does not support such top level attributes (total_count, limit, offset), you can set the @nometa@ parameter or @X-Redmine-Nometa@ HTTP header to 1 to get responses without them. Example:
99
100
<pre>
101
GET /issues.xml?nometa=1
102
103
<issues type="array">
104
  ...
105
</issues>
106
</pre>
107 23 Jean-Philippe Lang
108 29 Etienne Massip
h3. Fetching associated data
109
110
Since of version:1.1.0, you have to explicitly specify the associations you want to be included in the query result by appending the @include@ parameter to the query url :
111
112
Example:
113
114 41 Jean-Philippe Lang
To retrieve issue journals with its description:
115 29 Etienne Massip
116
<pre>
117
GET /issues/296.xml?include=journals
118
119
<issue>
120
  <id>296</id>
121 30 Etienne Massip
  ...
122 29 Etienne Massip
  <journals type="array">
123
  ...
124 1 Jean-Philippe Lang
  </journals>
125 41 Jean-Philippe Lang
</issue>
126
</pre>
127
128
You can also load multiple associations using a coma separated list of items.
129
130
Example:
131
132
<pre>
133
GET /issues/296.xml?include=journals,changesets
134
135
<issue>
136
  <id>296</id>
137
  ...
138
  <journals type="array">
139
  ...
140
  </journals>
141
  <changesets type="array">
142
  ...
143
  </changesets>
144 29 Etienne Massip
</issue>
145
</pre>
146
147 42 Jean-Philippe Lang
h3. Working with custom fields
148
149
Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes.
150
151
XML Example:
152
153
<pre>
154
GET /issues/296.xml      # an issue with 2 custom fields
155
156
<issue>
157
  <id>296</id>
158
  ...
159
  <custom_fields type="array">
160
    <custom_field name="Affected version" id="1">
161
      <value>1.0.1</value>
162
    </custom_field>
163
    <custom_field name="Resolution" id="2">
164
      <value>Fixed</value>
165
    </custom_field>
166
  </custom_fields>
167
</issue>
168
</pre>
169
170
JSON Example:
171
172
<pre>
173
GET /issues/296.json      # an issue with 2 custom fields
174
175
{"issue":
176
  {
177
    "id":8471,
178
    ...
179
    "custom_fields":
180
      [
181
        {"value":"1.0.1","name":"Affected version","id":1},
182
        {"value":"Fixed","name":"Resolution","id":2}
183
      ]
184
  }
185
}
186
</pre>
187
188
You can also set/change the values of the custom fields when creating/updating an object using the same syntax (except that the custom field name is not required).
189
190
XML Example:
191
192
<pre>
193
PUT /issues/296.xml
194
195
<issue>
196
  <subject>Updating custom fields of an issue</subject>
197
  ...
198
  <custom_fields type="array">
199
    <custom_field id="1">
200
      <value>1.0.2</value>
201
    </custom_field>
202
    <custom_field id="2">
203
      <value>Invalid</value>
204
    </custom_field>
205
  </custom_fields>
206
</issue>
207
</pre>
208
209
Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required.
210
211
JSON Example:
212
213
<pre>
214
PUT /issues/296.json
215
216
{"issue":
217
  {
218
    "subject":"Updating custom fields of an issue",
219
    ...
220
    "custom_fields":
221
      [
222
        {"value":"1.0.2","id":1},
223
        {"value":"Invalid","id":2}
224
      ]
225
  }
226
}
227
</pre>
228
229 61 Jean-Philippe Lang
h3. Attaching files
230
231
Support for adding attachments through the REST API is added in Redmine version:1.4.0.
232
233 93 Jean-Philippe Lang
First, you need to upload each file with a POST request to @/uploads.xml@ (or @/uploads.json@). The request body should be the content of the file you want to attach and the @Content-Type@ header must be set to @application/octet-stream@ (otherwise you'll get a @406 Not Acceptable@ response). If the upload succeeds, you get a 201 response that contains a token for your uploaded file.
234 1 Jean-Philippe Lang
235 93 Jean-Philippe Lang
Then you can use this token to attach your uploaded file to a new or an existing issue.
236
237
+XML Example+
238
239
First, upload your file:
240
241 61 Jean-Philippe Lang
<pre>
242
POST /uploads.xml
243
Content-Type: application/octet-stream
244
...
245
(request body is the file content)
246
247
# 201 response
248
<upload>
249
  <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
250
</upload>
251 1 Jean-Philippe Lang
</pre>
252 61 Jean-Philippe Lang
253 93 Jean-Philippe Lang
Then create the issue using the upload token:
254 61 Jean-Philippe Lang
255
<pre>
256
POST /issues.xml
257
<issue>
258
  <project_id>1</project_id>
259
  <subject>Creating an issue with a uploaded file</subject>
260 62 Jean-Philippe Lang
  <uploads type="array">
261 61 Jean-Philippe Lang
    <upload>
262
      <token>7167.ed1ccdb093229ca1bd0b043618d88743</token>
263
      <filename>image.png</filename>
264 86 Etienne Massip
      <description>An optional description here</description>
265 61 Jean-Philippe Lang
      <content_type>image/png</content_type>
266
    </upload>
267
  </uploads>
268
</issue>
269
</pre>
270
271 64 Jean-Philippe Lang
If you try to upload a file that exceeds the maximum size allowed, you get a 422 response:
272
273
<pre>
274
POST /uploads.xml
275
Content-Type: application/octet-stream
276
...
277
(request body larger than the maximum size allowed)
278
279
# 422 response
280
<errors>
281 1 Jean-Philippe Lang
  <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error>
282
</errors>
283 93 Jean-Philippe Lang
</pre>
284
285
+JSON Example+
286
287
First, upload your file:
288
289
<pre>
290
POST /uploads.json
291
Content-Type: application/octet-stream
292
...
293
(request body is the file content)
294
295
# 201 response
296
{"upload":{"token":"7167.ed1ccdb093229ca1bd0b043618d88743"}}
297
</pre>
298
299
Then create the issue using the upload token:
300
301
<pre>
302
POST /issues.json
303
{
304
  "issue": {
305
    "project_id": "1",
306
    "subject": "Creating an issue with a uploaded file",
307
    "uploads": [
308
      {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image.png", "content_type": "image/png"}
309
    ]
310
  }
311
}
312
</pre>
313
314
You can also upload multiple files (by doing multiple POST requests to @/uploads.json@), then create an issue with multiple attachments:
315
316
<pre>
317
POST /issues.json
318
{
319
  "issue": {
320
    "project_id": "1",
321
    "subject": "Creating an issue with a uploaded file",
322
    "uploads": [
323
      {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image1.png", "content_type": "image/png"},
324
      {"token": "7168.d595398bbb104ed3bba0eed666785cc6", "filename": "image2.png", "content_type": "image/png"}
325
    ]
326
  }
327
}
328 64 Jean-Philippe Lang
</pre>
329
330 59 Jean-Philippe Lang
h3. Validation errors
331
332
When trying to create or update an object with invalid or missing attribute parameters, you will get a @422 Unprocessable Entity@ response. That means that the object could not be created or updated. In such cases, the response body contains the corresponding error messages:
333
334
+XML Example+:
335
336
<pre>
337
# Request with invalid or missing attributes
338
POST /users.xml
339
<user>
340
  <login>john</login>
341
  <lastname>Smith</lastname>
342
  <mail>john</mail>
343
</uer>
344
345
# 422 response with the error messages in its body
346 65 Jean-Philippe Lang
<errors type="array">
347 59 Jean-Philippe Lang
  <error>First name can't be blank</error>
348
  <error>Email is invalid</error>
349
</errors>
350
</pre>
351
352
353
+JSON Example+:
354
355
<pre>
356
# Request with invalid or missing attributes
357
POST /users.json
358
{
359
  "user":{
360
    "login":"john",
361
    "lastname":"Smith",
362
    "mail":"john"
363
  }
364
}
365
366
# 422 response with the error messages in its body
367
{
368
  "errors":[
369
    "First name can't be blank",
370
    "Email is invalid"
371
  ]
372
}
373
</pre>
374
375 81 Jean-Philippe Lang
h3. JSONP Support
376
377 88 Jean-Philippe Lang
Redmine 2.1.0+ API supports "JSONP":http://en.wikipedia.org/wiki/JSONP to request data from a Redmine server in a different domain (say, with JQuery). The callback can be passed using the @callback@ or @jsonp@ parameter. As of Redmine 2.3.0, JSONP support is optional and disabled by default, you can enable it by checking *Enable JSONP support* in Administration -> Settings -> Authentication.
378 81 Jean-Philippe Lang
379
Example:
380
381
<pre>
382
GET /issues.json?callback=myHandler
383
384
myHandler({"issues":[ ... ]})
385
</pre>
386
387 1 Jean-Philippe Lang
h2. API Usage in various languages/tools
388 5 Jean-Philippe Lang
389 1 Jean-Philippe Lang
* [[Rest_api_with_ruby|Ruby]]
390
* [[Rest_api_with_php|PHP]]
391 23 Jean-Philippe Lang
* [[Rest_api_with_python|Python]]
392 27 Jean-Philippe Lang
* [[Rest_api_with_java|Java]]
393 1 Jean-Philippe Lang
* [[Rest_api_with_curl|cURL]]
394 37 Bevan Rudge
* "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine
395 48 Dorin Huzum
* [[Rest_api_with_csharp|.NET]]
396 49 Rodrigo Carvalho
* [[Rest_api_with_delphi|Delphi]]
397 54 Jean-Philippe Lang
398
h2. API Change history
399
400 58 Jean-Philippe Lang
This section lists changes to the existing API features only. New features of the API are listed in the [[Rest_api#API-Description|API Description]].
401 57 Jean-Philippe Lang
402 54 Jean-Philippe Lang
h3. 2012-01-29: Multiselect custom fields (r8721, version:1.4.0)
403
404
Custom fields with multiple values are now supported in Redmine and may be found in API responses. These custom fields have a @multiple=true attribute@ and their @value@ attribute is an array.
405
406
Example:
407
408
<pre>
409
GET /issues/296.json
410
411
{"issue":
412
  {
413
    "id":8471,
414
    ...
415
    "custom_fields":
416
      [
417
        {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1},
418
        {"value":"Fixed","name":"Resolution","id":2}
419
      ]
420
  }
421
}
422
</pre>