Rest api » History » Version 96
Go MAEDA, 2017-01-04 09:35
Added Files API
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 | 95 | Go MAEDA | |[[Rest_Search|Search]] | Alpha | | 3.3 | |
29 | 96 | Go MAEDA | |[[Rest_Files|Files]] | Alpha | | 3.4 | |
30 | 24 | Jean-Philippe Lang | |
31 | 15 | Eric Davis | Status legend: |
32 | 1 | Jean-Philippe Lang | |
33 | * Stable - feature complete, no major changes planned |
||
34 | * Beta - usable for integrations with some bugs or missing minor functionality |
||
35 | * Alpha - major functionality in place, needs feedback from API users and integrators |
||
36 | * Prototype - very rough implementation, possible major breaking changes mid-version. *Not recommended for integration* |
||
37 | * Planned - planned in a future version, depending on developer availability |
||
38 | |||
39 | 24 | Jean-Philippe Lang | h2. General topics |
40 | 1 | Jean-Philippe Lang | |
41 | 78 | Jean-Philippe Lang | h3. Specify @Content-Type@ on @POST@/@PUT@ requests |
42 | 66 | Etienne Massip | |
43 | 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@): |
44 | 82 | Jean-Philippe Lang | * for JSON content, it must be set to @Content-Type: application/json@. |
45 | 78 | Jean-Philippe Lang | * for XML content, to @Content-Type: application/xml@. |
46 | 66 | Etienne Massip | |
47 | 24 | Jean-Philippe Lang | h3. Authentication |
48 | |||
49 | 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: |
||
50 | * using your regular login/password via HTTP Basic authentication. |
||
51 | 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: |
52 | ** passed in as a "key" parameter |
||
53 | ** passed in as a username with a random password via HTTP Basic authentication |
||
54 | 46 | John Galambos | ** passed in as a "X-Redmine-API-Key" HTTP header (added in Redmine 1.1.0) |
55 | 38 | Jean-Philippe Lang | |
56 | You can find your API key on your account page ( /my/account ) when logged in, on the right-hand pane of the default layout. |
||
57 | 24 | Jean-Philippe Lang | |
58 | 89 | Jean-Philippe Lang | h3. User Impersonation |
59 | |||
60 | 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. |
||
61 | |||
62 | 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. |
63 | |||
64 | 24 | Jean-Philippe Lang | h3. Collection resources and pagination |
65 | |||
66 | 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: |
67 | 24 | Jean-Philippe Lang | |
68 | * @offset@: the offset of the first object to retrieve |
||
69 | * @limit@: the number of items to be present in the response (default is 25, maximum is 100) |
||
70 | |||
71 | Examples: |
||
72 | |||
73 | <pre> |
||
74 | GET /issues.xml |
||
75 | => returns the 25 first issues |
||
76 | |||
77 | GET /issues.xml?limit=100 |
||
78 | => returns the 100 first issues |
||
79 | |||
80 | GET /issues.xml?offset=30&limit=10 |
||
81 | => returns 10 issues from the 30th |
||
82 | </pre> |
||
83 | |||
84 | 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: |
||
85 | |||
86 | <pre> |
||
87 | GET /issues.xml |
||
88 | |||
89 | <issues type="array" total_count="2595" limit="25" offset="0"> |
||
90 | ... |
||
91 | </issues> |
||
92 | </pre> |
||
93 | |||
94 | <pre> |
||
95 | GET /issues.json |
||
96 | |||
97 | { "issues":[...], "total_count":2595, "limit":25, "offset":0 } |
||
98 | </pre> |
||
99 | |||
100 | 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: |
||
101 | |||
102 | <pre> |
||
103 | GET /issues.xml?nometa=1 |
||
104 | |||
105 | <issues type="array"> |
||
106 | ... |
||
107 | </issues> |
||
108 | </pre> |
||
109 | 23 | Jean-Philippe Lang | |
110 | 29 | Etienne Massip | h3. Fetching associated data |
111 | |||
112 | 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 : |
||
113 | |||
114 | Example: |
||
115 | |||
116 | 41 | Jean-Philippe Lang | To retrieve issue journals with its description: |
117 | 29 | Etienne Massip | |
118 | <pre> |
||
119 | GET /issues/296.xml?include=journals |
||
120 | |||
121 | <issue> |
||
122 | <id>296</id> |
||
123 | 30 | Etienne Massip | ... |
124 | 29 | Etienne Massip | <journals type="array"> |
125 | ... |
||
126 | 1 | Jean-Philippe Lang | </journals> |
127 | 41 | Jean-Philippe Lang | </issue> |
128 | </pre> |
||
129 | |||
130 | You can also load multiple associations using a coma separated list of items. |
||
131 | |||
132 | Example: |
||
133 | |||
134 | <pre> |
||
135 | GET /issues/296.xml?include=journals,changesets |
||
136 | |||
137 | <issue> |
||
138 | <id>296</id> |
||
139 | ... |
||
140 | <journals type="array"> |
||
141 | ... |
||
142 | </journals> |
||
143 | <changesets type="array"> |
||
144 | ... |
||
145 | </changesets> |
||
146 | 29 | Etienne Massip | </issue> |
147 | </pre> |
||
148 | |||
149 | 42 | Jean-Philippe Lang | h3. Working with custom fields |
150 | |||
151 | Most of the Redmine objects support custom fields. Their values can be found in the @custom_fields@ attributes. |
||
152 | |||
153 | XML Example: |
||
154 | |||
155 | <pre> |
||
156 | GET /issues/296.xml # an issue with 2 custom fields |
||
157 | |||
158 | <issue> |
||
159 | <id>296</id> |
||
160 | ... |
||
161 | <custom_fields type="array"> |
||
162 | <custom_field name="Affected version" id="1"> |
||
163 | <value>1.0.1</value> |
||
164 | </custom_field> |
||
165 | <custom_field name="Resolution" id="2"> |
||
166 | <value>Fixed</value> |
||
167 | </custom_field> |
||
168 | </custom_fields> |
||
169 | </issue> |
||
170 | </pre> |
||
171 | |||
172 | JSON Example: |
||
173 | |||
174 | <pre> |
||
175 | GET /issues/296.json # an issue with 2 custom fields |
||
176 | |||
177 | {"issue": |
||
178 | { |
||
179 | "id":8471, |
||
180 | ... |
||
181 | "custom_fields": |
||
182 | [ |
||
183 | {"value":"1.0.1","name":"Affected version","id":1}, |
||
184 | {"value":"Fixed","name":"Resolution","id":2} |
||
185 | ] |
||
186 | } |
||
187 | } |
||
188 | </pre> |
||
189 | |||
190 | 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). |
||
191 | |||
192 | XML Example: |
||
193 | |||
194 | <pre> |
||
195 | PUT /issues/296.xml |
||
196 | |||
197 | <issue> |
||
198 | <subject>Updating custom fields of an issue</subject> |
||
199 | ... |
||
200 | <custom_fields type="array"> |
||
201 | <custom_field id="1"> |
||
202 | <value>1.0.2</value> |
||
203 | </custom_field> |
||
204 | <custom_field id="2"> |
||
205 | <value>Invalid</value> |
||
206 | </custom_field> |
||
207 | </custom_fields> |
||
208 | </issue> |
||
209 | </pre> |
||
210 | |||
211 | Note: the @type="array"@ attribute on @custom_fields@ XML tag is strictly required. |
||
212 | |||
213 | JSON Example: |
||
214 | |||
215 | <pre> |
||
216 | PUT /issues/296.json |
||
217 | |||
218 | {"issue": |
||
219 | { |
||
220 | "subject":"Updating custom fields of an issue", |
||
221 | ... |
||
222 | "custom_fields": |
||
223 | [ |
||
224 | {"value":"1.0.2","id":1}, |
||
225 | {"value":"Invalid","id":2} |
||
226 | ] |
||
227 | } |
||
228 | } |
||
229 | </pre> |
||
230 | |||
231 | 61 | Jean-Philippe Lang | h3. Attaching files |
232 | |||
233 | Support for adding attachments through the REST API is added in Redmine version:1.4.0. |
||
234 | |||
235 | 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. |
236 | 1 | Jean-Philippe Lang | |
237 | 93 | Jean-Philippe Lang | Then you can use this token to attach your uploaded file to a new or an existing issue. |
238 | |||
239 | +XML Example+ |
||
240 | |||
241 | First, upload your file: |
||
242 | |||
243 | 61 | Jean-Philippe Lang | <pre> |
244 | POST /uploads.xml |
||
245 | Content-Type: application/octet-stream |
||
246 | ... |
||
247 | (request body is the file content) |
||
248 | |||
249 | # 201 response |
||
250 | <upload> |
||
251 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
252 | </upload> |
||
253 | 1 | Jean-Philippe Lang | </pre> |
254 | 61 | Jean-Philippe Lang | |
255 | 93 | Jean-Philippe Lang | Then create the issue using the upload token: |
256 | 61 | Jean-Philippe Lang | |
257 | <pre> |
||
258 | POST /issues.xml |
||
259 | <issue> |
||
260 | <project_id>1</project_id> |
||
261 | <subject>Creating an issue with a uploaded file</subject> |
||
262 | 62 | Jean-Philippe Lang | <uploads type="array"> |
263 | 61 | Jean-Philippe Lang | <upload> |
264 | <token>7167.ed1ccdb093229ca1bd0b043618d88743</token> |
||
265 | <filename>image.png</filename> |
||
266 | 86 | Etienne Massip | <description>An optional description here</description> |
267 | 61 | Jean-Philippe Lang | <content_type>image/png</content_type> |
268 | </upload> |
||
269 | </uploads> |
||
270 | </issue> |
||
271 | </pre> |
||
272 | |||
273 | 64 | Jean-Philippe Lang | If you try to upload a file that exceeds the maximum size allowed, you get a 422 response: |
274 | |||
275 | <pre> |
||
276 | POST /uploads.xml |
||
277 | Content-Type: application/octet-stream |
||
278 | ... |
||
279 | (request body larger than the maximum size allowed) |
||
280 | |||
281 | # 422 response |
||
282 | <errors> |
||
283 | 1 | Jean-Philippe Lang | <error>This file cannot be uploaded because it exceeds the maximum allowed file size (1024000)</error> |
284 | </errors> |
||
285 | 93 | Jean-Philippe Lang | </pre> |
286 | |||
287 | +JSON Example+ |
||
288 | |||
289 | First, upload your file: |
||
290 | |||
291 | <pre> |
||
292 | POST /uploads.json |
||
293 | Content-Type: application/octet-stream |
||
294 | ... |
||
295 | (request body is the file content) |
||
296 | |||
297 | # 201 response |
||
298 | {"upload":{"token":"7167.ed1ccdb093229ca1bd0b043618d88743"}} |
||
299 | </pre> |
||
300 | |||
301 | Then create the issue using the upload token: |
||
302 | |||
303 | <pre> |
||
304 | POST /issues.json |
||
305 | { |
||
306 | "issue": { |
||
307 | "project_id": "1", |
||
308 | "subject": "Creating an issue with a uploaded file", |
||
309 | "uploads": [ |
||
310 | {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image.png", "content_type": "image/png"} |
||
311 | ] |
||
312 | } |
||
313 | } |
||
314 | </pre> |
||
315 | |||
316 | You can also upload multiple files (by doing multiple POST requests to @/uploads.json@), then create an issue with multiple attachments: |
||
317 | |||
318 | <pre> |
||
319 | POST /issues.json |
||
320 | { |
||
321 | "issue": { |
||
322 | "project_id": "1", |
||
323 | "subject": "Creating an issue with a uploaded file", |
||
324 | "uploads": [ |
||
325 | {"token": "7167.ed1ccdb093229ca1bd0b043618d88743", "filename": "image1.png", "content_type": "image/png"}, |
||
326 | {"token": "7168.d595398bbb104ed3bba0eed666785cc6", "filename": "image2.png", "content_type": "image/png"} |
||
327 | ] |
||
328 | } |
||
329 | } |
||
330 | 64 | Jean-Philippe Lang | </pre> |
331 | |||
332 | 59 | Jean-Philippe Lang | h3. Validation errors |
333 | |||
334 | 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: |
||
335 | |||
336 | +XML Example+: |
||
337 | |||
338 | <pre> |
||
339 | # Request with invalid or missing attributes |
||
340 | POST /users.xml |
||
341 | <user> |
||
342 | <login>john</login> |
||
343 | <lastname>Smith</lastname> |
||
344 | <mail>john</mail> |
||
345 | </uer> |
||
346 | |||
347 | # 422 response with the error messages in its body |
||
348 | 65 | Jean-Philippe Lang | <errors type="array"> |
349 | 59 | Jean-Philippe Lang | <error>First name can't be blank</error> |
350 | <error>Email is invalid</error> |
||
351 | </errors> |
||
352 | </pre> |
||
353 | |||
354 | |||
355 | +JSON Example+: |
||
356 | |||
357 | <pre> |
||
358 | # Request with invalid or missing attributes |
||
359 | POST /users.json |
||
360 | { |
||
361 | "user":{ |
||
362 | "login":"john", |
||
363 | "lastname":"Smith", |
||
364 | "mail":"john" |
||
365 | } |
||
366 | } |
||
367 | |||
368 | # 422 response with the error messages in its body |
||
369 | { |
||
370 | "errors":[ |
||
371 | "First name can't be blank", |
||
372 | "Email is invalid" |
||
373 | ] |
||
374 | } |
||
375 | </pre> |
||
376 | |||
377 | 81 | Jean-Philippe Lang | h3. JSONP Support |
378 | |||
379 | 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. |
380 | 81 | Jean-Philippe Lang | |
381 | Example: |
||
382 | |||
383 | <pre> |
||
384 | GET /issues.json?callback=myHandler |
||
385 | |||
386 | myHandler({"issues":[ ... ]}) |
||
387 | </pre> |
||
388 | |||
389 | 1 | Jean-Philippe Lang | h2. API Usage in various languages/tools |
390 | 5 | Jean-Philippe Lang | |
391 | 1 | Jean-Philippe Lang | * [[Rest_api_with_ruby|Ruby]] |
392 | * [[Rest_api_with_php|PHP]] |
||
393 | 23 | Jean-Philippe Lang | * [[Rest_api_with_python|Python]] |
394 | 94 | Go MAEDA | * [[Rest_api_with_perl|Perl]] |
395 | 27 | Jean-Philippe Lang | * [[Rest_api_with_java|Java]] |
396 | 1 | Jean-Philippe Lang | * [[Rest_api_with_curl|cURL]] |
397 | 37 | Bevan Rudge | * "Drupal Redmine API module, 2.x branch (currently not stable)":http://drupal.org/project/redmine |
398 | 48 | Dorin Huzum | * [[Rest_api_with_csharp|.NET]] |
399 | 49 | Rodrigo Carvalho | * [[Rest_api_with_delphi|Delphi]] |
400 | 54 | Jean-Philippe Lang | |
401 | h2. API Change history |
||
402 | |||
403 | 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]]. |
404 | 57 | Jean-Philippe Lang | |
405 | 54 | Jean-Philippe Lang | h3. 2012-01-29: Multiselect custom fields (r8721, version:1.4.0) |
406 | |||
407 | 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. |
||
408 | |||
409 | Example: |
||
410 | |||
411 | <pre> |
||
412 | GET /issues/296.json |
||
413 | |||
414 | {"issue": |
||
415 | { |
||
416 | "id":8471, |
||
417 | ... |
||
418 | "custom_fields": |
||
419 | [ |
||
420 | {"value":["1.0.1","1.0.2"],"multiple":true,"name":"Affected version","id":1}, |
||
421 | {"value":"Fixed","name":"Resolution","id":2} |
||
422 | ] |
||
423 | } |
||
424 | } |
||
425 | </pre> |