Wiki source code of The XWiki RESTful API

Version 35.1 by Fabio Mancinelli on 2011/11/08

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc depth="2"/}}
3 {{/box}}
4
5 XWiki provides fine-grain access to virtually every element through an API that is based on HTTP semantics, i.e., a RESTful API. In this page you will find all the details to take advantage of this API and the instructions to use it at its full potential.
6
7 = Accessing the service =
8
9 By defaut the XWiki RESTful API entrypoint is rooted at the following URI:
10
11 {{code}}
12
13 http://host:port/xwiki/rest
14
15 {{/code}}
16
17 All the resource references described in the [[XWiki RESTful API Documentation>>#HXWikiRESTfulAPIDocumentation]] should be intended relative to this URL.
18
19 For example the ##/wikis## resources on a server running on ##localhost## on port ##8080## can be retrieved using the following URL : ##http:~/~/localhost:8080/xwiki/rest/wikis##
20
21 In addition to retrieving content in XML format, you can also retrieve it in JSON format by adding the parameter ##?media=json## in the URL. For example: ##http:~/~/localhost:8080/xwiki/rest/wikis?media=json##
22
23 = Dataset =
24
25 This section contains a brief and high-level description of the XWiki data set that should serve as a basis for presenting resources and their associated operations.
26
27 XWiki has **pages** organized in **spaces**. Each **page** is available in multiple **versions** (its **history**) and **translations**. Translated pages have their own **versions** and **history** which are independent. Each page might have **attachments**. Each attachment has its own **history**. Attachments are shared among all the different translations of a page (i.e., the same set of attachments is the same regardless of the page language). Pages can have one or more **objects**. Objects are instances of a **class** that contains a set of **properties**. Some objects might be directly exposed as first class entities, such as **comments** and **tags**. Objects, as attachments, are shared among all page translations.
28
29 = Understanding resources and representations =
30
31 "An important concept in REST is the existence of resources (sources of specific information), each of which is referenced with a global identifier (e.g., an URI in HTTP). In order to manipulate these resources, components of the network (user agents and origin servers) communicate via a standardized interface (e.g., HTTP) and exchange representations of these resources (the actual documents conveying the information)." ([[Wikipedia>>http://en.wikipedia.org/wiki/Representational_State_Transfer#REST.27s_central_principle:_resources]])
32
33 Resources in XWiki are pages, attachments, objects, properties, spaces, and all the //things// we described in the previous section. XWiki has a default way of conveying the information about these resources, i.e., by providing well defined XML representations that contain all the information associated to the resource in an XML format. This format is described using an XML Schema Definition file that can be found here: [[https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-model/src/main/resources/xwiki.rest.model.xsd]]
34
35 Of course the same resource can be represented in many different ways. This is yet to be documented.
36
37 Another important aspect of representations is that they contain useful information for linking related resources. This is a realization of the //Hypermedia As The Engine Of The Application State (HATEOAS)// principle. In XML representations this information is conveyed through the ##<link>## tag. This tag has two important parameters: **rel** and **href**. **rel** specifies the "semantics" of the link, while **href** is the URI of the linked resource.
38
39 For example, in the representation of a page, we can have links to the comments, tags, attachments which are independent resources associated to the current page. These links are provided in the XML representation of a page and allow a client to navigate to related resources... Like we do every day when we click on a link in a web page.
40
41 [[image:representation||height="430"]]
42
43 == Relations ==
44
45 The available relations that you might find in the XML resource representations are the following:
46
47 |=Rel|=Semantics
48 |{{{http://www.xwiki.org/rel/wikis}}}|The representation containing the list of virtual wikis.
49 |{{{http://www.xwiki.org/rel/spaces}}}|The representation containing the list of spaces in a wiki.
50 |{{{http://www.xwiki.org/rel/pages}}}|The representation containing the list of pages in a space.
51 |{{{http://www.xwiki.org/rel/translation}}}|The representation containing a translation of a page.
52 |{{{http://www.xwiki.org/rel/page}}}|The representation for a page.
53 |{{{http://www.xwiki.org/rel/space}}}|The representation for a space.
54 |{{{http://www.xwiki.org/rel/parent}}}|The representation for the page that is parent of the current resource.
55 |{{{http://www.xwiki.org/rel/home}}}|The representation for the page that is the home of the current resource.
56 |{{{http://www.xwiki.org/rel/attachmentData}}}|The representation of the actual attachment data.
57 |{{{http://www.xwiki.org/rel/comments}}}|The representation of the list of comments associated to the current resource.
58 |{{{http://www.xwiki.org/rel/attachments}}}|The representation of the list of attachments associated to the current resource.
59 |{{{http://www.xwiki.org/rel/objects}}}|The representation of the list of objects associated to the current resource.
60 |{{{http://www.xwiki.org/rel/object}}}|The representation for an object.
61 |{{{http://www.xwiki.org/rel/classes}}}|The representation of the list of classes associated to the current resource.
62 |{{{http://www.xwiki.org/rel/history}}}|The representation of the list of history information associated to the current resource.
63 |{{{http://www.xwiki.org/rel/class}}}|The representation for a class.
64 |{{{http://www.xwiki.org/rel/property}}}|The representation for a property.
65 |{{{http://www.xwiki.org/rel/properties}}}|The representation of the list of properties associated to the current resource.
66 |{{{http://www.xwiki.org/rel/modifications}}}|The representation of the list of modifications associated to the current resource.
67 |{{{http://www.xwiki.org/rel/children}}}|The representation of the list of children associated to the current resource.
68 |{{{http://www.xwiki.org/rel/tags}}}|The representation of the list of tags associated to the current resource.
69 |{{{http://www.xwiki.org/rel/tag}}}|The representation of a tag.
70 |{{{http://www.xwiki.org/rel/search}}}|The representation for a search resource.
71 |{{{http://www.xwiki.org/rel/syntaxes}}}|The representation for a syntax resource.
72
73 Relations are defined as URIs in order to provide a sort of namespace. Currently these URIs are not links to real web pages but, in the future, they might point to descriptions of their semantics on actual web pages (or other kinds of representations).
74
75 == The "HATEOAS" Graph ==
76
77 In order to better understand the relations among resources you might have a look at this [[graph>>attach:XWikiHATEOAS.pdf]] that pictures all the resources available in the XWiki RESTful API and the relations among them. In this graph, nodes are [[URI templates>>http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-03.txt]] representing classes of resources. Edges are the possible links that you might find in a representation of a given resource, and their associated relations.
78
79 This graph shows that by starting from the API entry-point a client can navigate and discover all the resources just by following the links provided in representations (and by knowing their semantics). This was exactly the way how this graph was generated.
80
81 = Interacting with the XWiki RESTful API =
82
83 The XWiki RESTful API is accessible through HTTP so, in principle, you can use every client that is capable of "speaking" HTTP in order to interact with it. Even a web browser!
84 If you want to write more complex programs you might download an HTTP library for your favorite language (e.g., [[http://hc.apache.org/]]).
85
86 Java users might take advantage of the [[JAXB>>https://jaxb.dev.java.net]] framework and its [[XJC binding compiler>>https://jaxb.dev.java.net/jaxb20-ea3/docs/xjc.html]] in order to generate domain object models directly from the [[XML Schema Definition>>https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-model/src/main/resources/xwiki.rest.model.xsd]], and use them for serializing and de-serializing XML representations.
87
88 If you use this approach (Apache HTTP Client + JAXB) you will find yourself writing some code like this:
89
90 {{code language="java"}}
91 import javax.xml.bind.JAXBContext;
92 import javax.xml.bind.Unmarshaller;
93
94 import org.apache.commons.httpclient.HttpClient;
95 import org.apache.commons.httpclient.methods.GetMethod;
96 import org.xwiki.rest.model.jaxb.Page;
97
98 ...
99 HttpClient httpClient = new HttpClient();
100 JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
101 Unmarshaller unmarshaller = context.createUnmarshaller();
102
103 GetMethod getMethod = new GetMethod("http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome");
104 getMethod.addRequestHeader("Accept", "application/xml");
105 httpClient.executeMethod(getMethod);
106
107 Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
108 {{/code}}
109
110 And you will have all the information about the Main.WebHome page in the Page object, without the need of handling XML directly.
111
112 Because of the wide variety of HTTP frameworks available we don't provide a full tutorial about using them. However, in order to show you how to interact with the XWiki RESTful API, we will use [[curl>>http://curl.haxx.se]]: a standard command line HTTP client that provides an interface to all the functionalities of the HTTP protocol.
113
114 By using curl, the previous example would have been:
115
116 {{code language="xml"}}
117 $ curl http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome
118 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
119 <page xmlns="http://www.xwiki.org">
120 <link rel="http://www.xwiki.org/rel/space" href="http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main"/>
121 ...
122 {{/code}}
123
124 == Authentication ==
125
126 The XWiki RESTful API supports two types of authentication:
127
128 * **HTTP BASIC Auth**: You provide your credentials using the Authorization HTTP header
129 * **XWiki session**: If you are logged in XWiki and you use the cookies provided by the authentication mechanism, you will also be authenticated to the XWiki RESTful API. This is useful, for example, when you are interacting with the API using the XMLHttpRequest object of a browser using Javascript.
130
131 If you don't provide any credentials the XWiki RESTful API will recognize you as a XWiki.Guest user.
132
133 So if you have, let's say a Main.PrivatePage, and you try to do:
134
135 {{code language="none"}}
136 $ curl -v http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/PrivatePage
137 ...
138 < HTTP/1.1 401 Unauthorized
139 ...
140 {{/code}}
141
142 You will get an Unauthorized empty response.
143
144 On the contrary, by specifying Admin credentials you gain access to the actual page:
145
146 {{code language="xml"}}
147 $ curl -u Admin:admin http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/PrivatePage
148 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
149 <page xmlns="http://www.xwiki.org">
150 <link rel="http://www.xwiki.org/rel/space" href="http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main"/>
151 ...
152 <content>Only admin can see this</content>
153 </page>
154 {{/code}}
155
156 == Sending representations ==
157
158 Many resources are modifiable, so you can send representations in order to change the state of those resources (e.g., pages).
159 All modifiable resources accept XML representations that conform to the [[XML Schema Definition>>https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-model/src/main/resources/xwiki.rest.model.xsd]]. However, some other representations might be accepted as well (see the following sections).
160
161 Resource update is usually done by using the PUT method, while resource creation is done via PUT or POST.
162
163 For example, in order to create a page you might do the following:
164
165 {{code language="xml"}}
166 $ curl -u Admin:admin -X PUT --data-binary "@newpage.xml" -H "Content-Type: application/xml" http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/NewPage
167 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
168 <page xmlns="http://www.xwiki.org">
169 <link rel="http://www.xwiki.org/rel/space" href="http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main"/>
170 ...
171 <version>1.1</version>
172 <majorVersion>1</majorVersion>
173 <minorVersion>1</minorVersion>
174 <created>2009-03-21+01:00</created>
175 <creator>XWiki.Admin</creator>
176 <modified>2009-03-21+01:00</modified>
177 <modifier>XWiki.Admin</modifier>
178 <content>This is a new page</content>
179 </page>
180 {{/code}}
181
182 Where newpage.xml is an XML file containing
183
184 {{code language="xml"}}
185 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
186 <page xmlns="http://www.xwiki.org">
187 <title>Hello world</title>
188 <syntax>xwiki/2.0</syntax>
189 <content>This is a new page</content>
190 </page>
191 {{/code}}
192
193 The page has been created and is accessible. Subsequent PUT requests to the page URI will modify its content.
194
195 You can specify a subset of the three elements {{{title}}}, {{{syntax}}}, and {{{content}}} in the XML when updating/creating a page.
196 For example, if you just want to change the title, it is sufficient to specify only the {{{title}}} element. The current content and the syntax of the page will be left unchanged.
197
198 == Overcoming browser limitations ==
199
200 As said before, it could be useful to send information by using browser's XmlHttpRequest objects. However, currently many browsers only support GET and POST methods, so it is impossible to send, for example, PUT requests. In order to overcome this limitation you can override the HTTP Method by specifying a ##method## parameter in the URI query string.
201
202 In the previous example, if you send a POST request to the ##[[http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/NewPage?method=PUT]]## it will be interpreted as if it were an actual PUT request.
203
204 This overriding mechanism allows the interaction with the XWiki RESTful API by using any kind of browser.
205
206 == PUT vs POST ==
207
208 In the following sections you will see that sometimes resources are created by using PUT and sometimes by using POST. The general principle is that if the client is responsible for choosing the resource URI then PUT is used. If it's the server that bears this responsibility, then POST is used.
209
210 To be clearer, when a client wants to create a page it knows **where** that page should go, so it is able to communicate the server the target URI. PUT is used.
211
212 A client, on the contrary, cannot know beforehand what will be the URI of a comment, since comment URIs contains the ID of the comment and this information is generated by the server. In this case the client will do a POST and the server, in response, will communicate the URI it generated for the newly created comment.
213
214 = XWiki RESTful API Documentation =
215
216 In this section you will find the documentation of the whole XWiki RESTful API.
217
218 **application/xml** representations refers to the XML Schema Definition at the following location: [[https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-model/src/main/resources/xwiki.rest.model.xsd]]
219
220 Resource URIs are specified using [[URI templates>>http://bitworking.org/projects/URI-Templates/spec/draft-gregorio-uritemplate-03.txt]]. Bracketed elements are formal parameters and should be instantiated to actual values in order to retrieve the associated resource.
221
222 == Root resources ==
223
224 By defaut all the resources of the RESTful API are rooted at the following URI: ##http:~/~/server:port/xwiki/rest/## (depending on where your XWiki is running)
225
226 === / ===
227
228 * **HTTP Method:** GET
229 ** **Media types:**
230 *** application/xml (XWiki element)
231 ** **Description:** Retrieves the entry root description containing information about the server (currently returns the XWiki product Version).
232 ** **Status codes:**
233 *** 200: If the request was successful.
234
235 === /syntaxes
236
237 * **HTTP Method:** GET
238 ** **Media types:**
239 *** application/xml (Syntaxes element)
240 ** **Description:** The list of syntaxes supported by the XWiki instance.
241 ** **Status codes:**
242 *** 200: If the request was successful.
243
244 === /wikis ===
245
246 * **HTTP Method:** GET
247 ** **Media types:**
248 *** application/xml (Wikis element)
249 ** **Description:** The list of wikis available on the XWiki instance. Unless the wiki is configured to be a wiki farm, this list is usually made of a single element 'xwiki'.
250 ** **Status codes:**
251 *** 200: If the request was successful.
252
253 === /wikis/{wikiName}/search?q~={keywords}~[~[&scope~={name,content,title,objects}...]&number~=n] ===
254
255 * **HTTP Method:** GET
256 ** **Media types:**
257 *** application/xml (SearchResults element)
258 ** **Description:** The list of pages and objects that contain the {keywords} in the specified {scope}s. Multiple scopes can be specified. Search results are relative to the whole {wikiName}
259 ** **Status codes:**
260 *** 200: If the request was successful.
261
262 == Space resources ==
263
264 === /wikis/{wikiName}/spaces[?start~=offset&number~=n] ===
265
266 * **HTTP Method:** GET
267 ** **Media types:**
268 *** application/xml (Spaces element)
269 ** **Description:** Retrieves the list of spaces available in the {wikiName} wiki.
270 ** **Status codes:**
271 *** 200: If the request was successful.
272
273 === /wikis/{wikiName}/spaces/{spaceName}/search?q~={keywords}~[~[&scope~={name,content,title,objects}...]&number~=n] ===
274
275 * **HTTP Method:** GET
276 ** **Media types:**
277 *** application/xml (Search results element)
278 ** **Description:** The list of pages and objects that contain the {keywords} in the specified {scope}s. Multiple scopes can be specified. Search results are relative to space {spaceName}
279 ** **Status codes:**
280 *** 200: If the request was successful.
281 *** 401: If the user is not authorized.
282
283 == Page resources ==
284
285 === /wikis/{wikiName}/spaces/{spaceName}/pages[?start~=offset&number~=n] ===
286
287 * **HTTP Method:** GET
288 ** **Media types:**
289 *** application/xml (Pages element)
290 ** **Description:** The list of pages in the space {spaceName}
291 ** **Status codes:**
292 *** 200: If the request was successful.
293 *** 401: If the user is not authorized.
294
295 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName} ===
296
297 * **HTTP Method:** GET
298 ** **Media types:**
299 *** application/xml (Page element)
300 ** **Description:**
301 ** **Status codes:**
302 *** 200: If the request was successful.
303 *** 401: If the user is not authorized.
304
305 \\
306
307 * **HTTP Method:** PUT
308 ** **Accepted Media types:**
309 *** application/xml (Page element)
310 *** text/plain (Only page content)
311 *** application/x-www-form-urlencoded (allowed field names: title, parent, content)
312 ** **Media types:**
313 *** application/xml (Page element)
314 ** **Description:** Create or updates a page.
315 ** **Status codes:**
316 *** 201: If the page was created.
317 *** 202: If the page was updated.
318 *** 304: If the page was not modified.
319 *** 401: If the user is not authorized.
320
321 \\
322
323 * **HTTP Method:** DELETE
324 ** **Media types:**
325 *** application/xml (Page element)
326 ** **Description:** Delete the page.
327 ** **Status codes:**
328 *** 204: If the request was successful.
329 *** 401: If the user is not authorized.
330
331 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history[?start~=offset&number~=n] ===
332
333 * **HTTP Method:** GET
334 ** **Media types:**
335 *** application/xml (History element)
336 ** **Description:** The list of all the versions of the given page.
337 ** **Status codes:**
338 *** 200: If the request was successful.
339 *** 401: If the user is not authorized.
340
341 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version} ===
342
343 * **HTTP Method:** GET
344 ** **Media types:**
345 *** application/xml (Page element)
346 ** **Description:** The page at version {version}
347 ** **Status codes:**
348 *** 200: If the request was successful.
349 *** 401: If the user is not authorized.
350
351 ==== /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/translations[?start~=offset&number~=n] ====
352
353 * **HTTP Method:** GET
354 ** **Media types:**
355 *** application/xml (Translations element)
356 ** **Description:** The list of available translation for the page
357 ** **Status codes:**
358 *** 200: If the request was successful.
359 *** 401: If the user is not authorized.
360
361 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/translations/{language} ===
362
363 * **HTTP Method:** GET
364 ** **Media types:**
365 *** application/xml (Page element)
366 ** **Description:** The page at in the given {language}.
367 ** **Status codes:**
368 *** 200: If the request was successful.
369 *** 401: If the user is not authorized.
370
371 \\
372
373 * **HTTP Method:** PUT
374 ** **Accepted Media types:**
375 *** application/xml (Page element)
376 *** text/plain (Only page content)
377 *** application/x-www-form-urlencoded (allowed field names: title, parent, content)
378 ** **Media types:**
379 *** application/xml (Page element)
380 ** **Description:** Create or updates a page translation.
381 ** **Status codes:**
382 *** 201: If the page was created.
383 *** 202: If the page was updated.
384 *** 304: If the page was not modified.
385 *** 401: If the user is not authorized.
386
387 \\
388
389 * **HTTP Method:** DELETE
390 ** **Media types:**
391 *** application/xml (Page element)
392 ** **Description:** Delete the page translation.
393 ** **Status codes:**
394 *** 204: If the request was successful.
395 *** 401: If the user is not authorized.
396
397 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/translations/{language}/history ===
398
399 * **HTTP Method:** GET
400 ** **Media types:**
401 *** application/xml (History element)
402 ** **Description:** The list of all the available revisions of the page in a given {language}.
403 ** **Status codes:**
404 *** 200: If the request was successful.
405 *** 401: If the user is not authorized.
406
407 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/translations/{lang}/history/{version} ===
408
409 * **HTTP Method:** GET
410 ** **Media types:**
411 *** application/xml (Page element)
412 ** **Description:** A page at a given {version} in a given {language}.
413 ** **Status codes:**
414 *** 200: If the request was successful.
415 *** 401: If the user is not authorized.
416
417 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/children ===
418
419 * **HTTP Method:** GET
420 ** **Media types:**
421 *** application/xml (Pages element)
422 ** **Description:** The list of the children of a given page.
423 ** **Status codes:**
424 *** 200: If the request was successful.
425 *** 401: If the user is not authorized.
426
427 === /wikis/{wikiName}/pages[?name~=paneName&space~=spaceName&author~=authorName] ===
428
429 * **HTTP Method:** GET
430 ** **Media types:**
431 *** application/xml (Pages element)
432 ** **Description:** The list of pages in the wiki {wikiName}. Filters can be set for the name, space and/or author to include only pages that match the given filters. This resource can be used to search for pages in a wiki.
433 ** **Status codes:**
434 *** 200: If the request was successful.
435 *** 401: If the user is not authorized.
436
437 == Tag resources ==
438
439 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/tags ===
440
441 * **HTTP Method:** GET
442 ** **Media types:**
443 *** application/xml (Tags element)
444 ** **Description:** List page tags.
445 ** **Status codes:**
446 *** 200: If the request was successful.
447 *** 401: If the user is not authorized.
448
449 \\
450
451 * **HTTP Method:** PUT
452 ** **Accepted Media types:**
453 *** application/xml (Tag element)
454 *** text/plain
455 *** application/x-www-form-urlencoded (allowed field names: tag)
456 ** **Media types:**
457 *** application/xml (Tags element)
458 ** **Description:** Add a tag to the page.
459 ** **Status codes:**
460 *** 202: If the request was successful.
461 *** 401: If the user is not authorized.
462
463 === /wikis/{wikiName}/tags ===
464
465 * **HTTP Method:** GET
466 ** **Media types:**
467 *** application/xml (Tags element)
468 ** **Description:** The list of all available tags
469 ** **Status codes:**
470 *** 200: If the request was successful.
471 *** 401: If the user is not authorized.
472
473 === /wikis/{wikiName}/tags/{tag1}[,{tag2},{tag3}...][?start~=offset&number~=n] ===
474
475 * **HTTP Method:** GET
476 ** **Media types:**
477 *** application/xml (Pages element)
478 ** **Description:** The list of pages having the specified tags.
479 ** **Status codes:**
480 *** 200: If the request was successful.
481 *** 401: If the user is not authorized.
482
483 == Comments resources ==
484
485 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/comments[?start~=offset&number~=n] ===
486
487 * **HTTP Method:** GET
488 ** **Media types:**
489 *** application/xml (Comments element)
490 ** **Description:** The list of comments on a given page.
491 ** **Status codes:**
492 *** 200: If the request was successful.
493 *** 401: If the user is not authorized.
494
495 \\
496
497 * **HTTP Method:** POST
498 ** **Accepted Media types:**
499 *** application/xml (Comment element)
500 *** text/plain
501 *** application/x-www-form-urlencoded - allowed field names: ##text##, ##replyTo## (object number of the replied comment, since XE 2.3)
502 ** **Media types:**
503 *** application/xml (Comment element)
504 ** **Description:** Create a comment on the given page.
505 ** **Status codes:**
506 *** 201: If the comment was created. (The Location header will contain the URI where the comment has been created.)
507 *** 401: If the user is not authorized.
508
509 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/comments/{commentId} ===
510
511 * **HTTP Method:** GET
512 ** **Media types:**
513 *** application/xml (Comment element)
514 ** **Description:** A specific comment on a page
515 ** **Status codes:**
516 *** 200: If the request was successful.
517 *** 401: If the user is not authorized.
518
519 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/comments ===
520
521 * **HTTP Method:** GET
522 ** **Media types:**
523 *** application/xml (Comments element)
524 ** **Description:** The list of comments at a specific page {version}.
525 ** **Status codes:**
526 *** 200: If the request was successful.
527 *** 401: If the user is not authorized.
528
529 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/comments/{commentId} ===
530
531 * **HTTP Method:** GET
532 ** **Media types:**
533 *** application/xml (Comment element)
534 ** **Description:** A comment at a specific page {version}.
535 ** **Status codes:**
536 *** 200: If the request was successful.
537 *** 401: If the user is not authorized.
538
539 == Attachments resources ==
540
541 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/attachments[?start~=offset&number~=n] ===
542
543 * **HTTP Method:** GET
544 ** **Media types:**
545 *** application/xml (Attachments element)
546 ** **Description:** The list of attachments of a given page.
547 ** **Status codes:**
548 *** 200: If the request was successful.
549 *** 401: If the user is not authorized.
550
551 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/attachments/{attachmentName} ===
552
553 * **HTTP Method:** GET
554 ** **Media types:**
555 *** The same of the attachment media type.
556 ** **Description:** The attachment identified by {attachmentName} on a given page.
557 ** **Status codes:**
558 *** 200: If the request was successful.
559 *** 401: If the user is not authorized.
560
561 \\
562
563 * **HTTP Method:** PUT
564 ** **Accepted media types:**
565 *** **/**
566 ** **Media types:**
567 *** application/xml (AttachmentSummary element)
568 ** **Description:** Create an attachment identified by {attachmentName} on a given page.
569 ** **Status codes:**
570 *** 201: If the attachment was created.
571 *** 202: If the attachment was updated.
572 *** 401: If the user is not authorized.
573
574 \\
575
576 * **HTTP Method:** DELETE
577 ** **Media types:**
578 ** **Description:** Delete the attachment identified by {attachmentName} on a given page.
579 ** **Status codes:**
580 *** 204: If the attachment was deleted.
581 *** 401: If the user is not authorized.
582
583 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/attachments[?start~=offset&number~=n] ===
584
585 * **HTTP Method:** GET
586 ** **Media types:**
587 *** application/xml (Attachments element)
588 ** **Description:** The list of attachments at a given page {version}.
589 ** **Status codes:**
590 *** 200: If the request was successful.
591 *** 401: If the user is not authorized.
592
593 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/attachments/{attachmentName} ===
594
595 * **HTTP Method:** GET
596 ** **Media types:**
597 *** The same of the attachment media type.
598 ** **Description:** The attachment identified by {attachmentName} on a given page {version}.
599 ** **Status codes:**
600 *** 200: If the request was successful.
601 *** 401: If the user is not authorized.
602
603 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/attachments/{attachmentName}/history ===
604
605 * **HTTP Method:** GET
606 ** **Media types:**
607 *** application/xml (Attachments element)
608 ** **Description:** The list of available version for the {attachmentName}
609 ** **Status codes:**
610 *** 200: If the request was successful.
611 *** 401: If the user is not authorized.
612
613 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/attachments/{attachmentName}/history/{version} ===
614
615 * **HTTP Method:** GET
616 ** **Media types:**
617 *** The same of the attachment media type.
618 ** **Description:** The {attachmentName} at a given {version}
619 ** **Status codes:**
620 *** 200: If the request was successful.
621 *** 401: If the user is not authorized.
622
623 === /wikis/{wikiName}/spaces/{spaceName}/attachments[?name~=attachmentName&page~=pageName&author~=authorName&types~=attachmentTypeList&start~=offset&number~=n] ===
624
625 * **HTTP Method:** GET
626 ** **Media types:**
627 *** application/xml (Attachments element)
628 ** **Description:** The list of attachments of pages located in a given {spaceName}. Filters can be set for the name, page, author and/or types (comma separated list of strings) to include only attachments that match the given filters. This resource can be used to search for attachments in a space.
629 ** **Status codes:**
630 *** 200: If the request was successful.
631 *** 401: If the user is not authorized.
632
633 === /wikis/{wikiName}/attachments[?name~=attachmentName&page~=pageName&space~=spaceName&author~=authorName&types~=attachmentTypeList&start~=offset&number~=n] ===
634
635 * **HTTP Method:** GET
636 ** **Media types:**
637 *** application/xml (Attachments element)
638 ** **Description:** The list of attachments in a given {wikiName}. Filters can be set for the name, page, space, author and/or type (comma separated list of strings) to include only attachments that match the given filters. This resource can be used to search for attachments in a wiki.
639 ** **Status codes:**
640 *** 200: If the request was successful.
641 *** 401: If the user is not authorized.
642
643 == Object resources ==
644
645 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects[?start~=offset&number~=n] ===
646
647 * **HTTP Method:** GET
648 ** **Media types:**
649 *** application/xml (Objects element)
650 ** **Description:** The list of objects associated to a page.
651 ** **Status codes:**
652 *** 200: If the request was successful.
653 *** 401: If the user is not authorized.
654
655 \\
656
657 * **HTTP Method:** POST
658 ** **Accepted media types:**
659 *** application/xml (Object element)
660 *** application/x-www-form-urlencoded (a set of property#name=value pairs representing properties and a field className)
661 ** **Media types:**
662 *** application/xml (Object element)
663 ** **Description:** Create a new object.
664 ** **Status codes:**
665 *** 201: If the object was created (The Location header will contain the URI associated to the newly created object).
666 *** 401: If the user is not authorized.
667
668 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects/{className}[?start~=offset&number~=n] ===
669
670 * **HTTP Method:** GET
671 ** **Media types:**
672 *** application/xml (Objects element)
673 ** **Description:** The list of objects of a given {className} associated to a page.
674 ** **Status codes:**
675 *** 200: If the request was successful.
676 *** 401: If the user is not authorized.
677
678 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects/{className}/{objectNumber} ===
679
680 * **HTTP Method:** GET
681 ** **Media types:**
682 *** application/xml (Object element)
683 ** **Description:** The object of type {className} identified by {objectNumber} associated to the given page.
684 ** **Status codes:**
685 *** 200: If the request was successful.
686 *** 401: If the user is not authorized.
687
688 \\
689
690 * **HTTP Method:** PUT
691 ** **Accepted media types:**
692 *** application/xml (Object element)
693 *** application/x-www-form-urlencoded (a set of property#name=value pairs representing properties)
694 ** **Media types:**
695 *** application/xml (Object element)
696 ** **Description:** Modify the object properties.
697 ** **Status codes:**
698 *** 202: If the object was updated.
699 *** 401: If the user is not authorized.
700
701 \\
702
703 * **HTTP Method:** DELETE
704 ** **Media types:**
705 ** **Description:** Delete the object.
706 ** **Status codes:**
707 *** 204: If the object was deleted.
708 *** 401: If the user is not authorized.
709
710 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects/{className}/{objectNumber}/properties ===
711
712 * **HTTP Method:** GET
713 ** **Media types:**
714 *** application/xml (Properties element)
715 ** **Description:** The properties of the object of type {className} identified by {objectNumber} associated to the given page.
716 ** **Status codes:**
717 *** 200: If the request was successful.
718 *** 401: If the user is not authorized.
719
720 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects/{className}/{objectNumber}/properties/{propertyName} ===
721
722 * **HTTP Method:** GET
723 ** **Media types:**
724 *** application/xml (Properties element)
725 ** **Description:** The property {propertyname} of the object of type {className} identified by {objectNumber} associated to the given page.
726 ** **Status codes:**
727 *** 200: If the request was successful.
728 *** 401: If the user is not authorized.
729
730 \\
731
732 * **HTTP Method:** PUT
733 ** **Accepted media types:**
734 *** application/xml (Property element)
735 *** text/plain
736 *** application/x-www-form-urlencoded (a field property#name=value pairs representing a property)
737 ** **Media types:**
738 *** application/xml (Property element)
739 ** **Description:** Modify the object properties.
740 ** **Status codes:**
741 *** 202: If the object was updated.
742 *** 401: If the user is not authorized.
743
744 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/objects[?start~=offset&number~=n] ===
745
746 * **HTTP Method:** GET
747 ** **Media types:**
748 *** application/xml (Objects element)
749 ** **Description:** The list of objects associated to a page at a given {version}.
750 ** **Status codes:**
751 *** 200: If the request was successful.
752 *** 401: If the user is not authorized.
753
754 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/objects/{className}/{objectNumber} ===
755
756 * **HTTP Method:** GET
757 ** **Media types:**
758 *** application/xml (Object element)
759 ** **Description:** The object of type {className} identified by {objectNumber} associated to the given page at a given {version}.
760 ** **Status codes:**
761 *** 200: If the request was successful.
762 *** 401: If the user is not authorized.
763
764 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/objects/{className}/{objectNumber}/properties ===
765
766 * **HTTP Method:** GET
767 ** **Media types:**
768 *** application/xml (Properties element)
769 ** **Description:** The properties of the object of type {className} identified by {objectNumber} associated to the given page at a given {version}.
770 ** **Status codes:**
771 *** 200: If the request was successful.
772 *** 401: If the user is not authorized.
773
774 === /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/history/{version}/objects/{className}/{objectNumber}/properties/{propertyName} ===
775
776 * **HTTP Method:** GET
777 ** **Media types:**
778 *** application/xml (Properties element)
779 ** **Description:** The property {propertyname} of the object of type {className} identified by {objectNumber} associated to the given page at a given {version}.
780 ** **Status codes:**
781 *** 200: If the request was successful.
782 *** 401: If the user is not authorized.
783
784 === /wikis/{wikiName}/class/{className}/objects ===
785
786 * **HTTP Method:** GET
787 ** **Media types:**
788 *** application/xml (Objects element)
789 ** **Description:** The list of all the objects of a given {className}.
790 ** **Status codes:**
791 *** 200: If the request was successful.
792 *** 401: If the user is not authorized.
793
794 == Class resources ==
795
796 === /wikis/{wikiName}/classes[?start~=offset&number~=n] ===
797
798 * **HTTP Method:** GET
799 ** **Media types:**
800 *** application/xml (Classes element)
801 ** **Description:** The list of all the classes defined in the wiki {wikiName}
802 ** **Status codes:**
803 *** 200: If the request was successful.
804 *** 401: If the user is not authorized.
805
806 === /wikis/{wikiName}/classes/{className} ===
807
808 * **HTTP Method:** GET
809 ** **Media types:**
810 *** application/xml (Class element)
811 ** **Description:** The {className} definition
812 ** **Status codes:**
813 *** 200: If the request was successful.
814 *** 401: If the user is not authorized.
815
816 === /wikis/{wikiName}/classes/{className}/properties ===
817
818 * **HTTP Method:** GET
819 ** **Media types:**
820 *** application/xml (Properties element)
821 ** **Description:** The properties of the class {className}.
822 ** **Status codes:**
823 *** 200: If the request was successful.
824 *** 401: If the user is not authorized.
825
826 === /wikis/{wikiName}/classes/{className}/properties/{property} ===
827
828 * **HTTP Method:** GET
829 ** **Media types:**
830 *** application/xml (Property element)
831 ** **Description:** The property {property} of the class {className}.
832 ** **Status codes:**
833 *** 200: If the request was successful.
834 *** 401: If the user is not authorized.
835
836 == Other resources ==
837
838 === /wikis/{wikiName}/modifications[?start~=offset&number~=n&date~=t] ===
839
840 * **HTTP Method:** GET
841 ** **Media types:**
842 *** application/xml (Modifications element)
843 ** **Description:** The list of the latest modification made to the wiki {wikiName} starting from time t (t is expressed in milliseconds from 1970 of the starting date)
844 ** **Status codes:**
845 *** 200: If the request was successful.
846 *** 401: If the user is not authorized.
847
848 = Custom resources =
849
850 It's possible to easily add any REST resource by registering a ##org.xwiki.rest.XWikiResource## java component on your wiki (see [[Component guide>>DevGuide.WritingComponents]] for more details).
851
852 {{code language="java"}}
853 package org.xwiki.contrib.rest;
854
855 import javax.ws.rs.DefaultValue;
856 import javax.ws.rs.GET;
857 import javax.ws.rs.Path;
858
859 import org.xwiki.component.annotation.Component;
860 import org.xwiki.rest.XWikiResource;
861
862 @Component("org.xwiki.contrib.rest.HelloWordResource")
863 @Path("/myresources/{myresourcename}")
864 class HelloWorldResource extends XWikiResource {
865 @GET
866 public String get(@PathParam("myresourcename") @DefaultValue("world") String myresourcename)
867 {
868 return "Hello " + myresourcename;
869 }
870 }
871 {{/code}}
872
873 The name of the component has to be the class FQN.
874
875 You can find more examples on [[https://github.com/xwiki/xwiki-platform/tree/master/xwiki-platform-core/xwiki-platform-rest/xwiki-platform-rest-server/src/main/java/org/xwiki/rest/resources]].
876
877 = Using the RESTful API =
878
879 == Highlevel description and tutorial for a basic usage of the RESTful API ==
880
881 See this [[tutorial by Fabio Mancinelli>>http://blog.fabiomancinelli.org/2011/03/07/XWikis_RESTful_API.html]].
882
883 == Creating an XWiki Object ==
884
885 In this example we will use the [[curl>>http://curl.haxx.se/]] utility as the HTTP client.
886
887 Imagine that you want to create on the page Test.Test a new object of the class XWiki.TestClass, supposing that the class has a text property.
888
889 So, on the command line, you have to do the following:
890
891 {{code}}
892 $ curl -u Admin:admin
893 -X POST
894 -H "Content-type: application/xml"
895 -H "Accept: application/xml"
896 -d "@test.xml"
897 http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
898 {{/code}}
899
900 where ##test.xml## is:
901
902 {{code language="xml"}}
903 <object xmlns="http://www.xwiki.org">
904 <className>XWiki.TestClass</className>
905 <property name="Text">
906 <value>Whatever you want to put here</value>
907 </property>
908 </object>
909 {{/code}}
910
911 Alternatively you can use the less verbose ##application/x-www-form-urlencoded format##:
912
913 {{code}}
914 $ curl -u Admin:admin
915 -X POST
916 -H "Content-type: application/x-www-form-urlencoded"
917 -H "Accept: application/xml"
918 -d "@test.txt"
919 http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
920 {{/code}}
921
922 where ##test.txt## contains something like:
923
924 {{code}}
925 className=XWiki.TestClass&property#Test=Whatever+you+want
926 {{/code}}
927
928 Or, better, you can use directly curl to specify these parameters
929 using multiple ##-d## switches:
930
931 {{code}}
932 $ curl -u Admin:admin
933 -X POST -H "Content-type: application/x-www-form-urlencoded"
934 -H "Accept: application/xml"
935 -d "className=XWiki.TestClass"
936 -d "property#Test=Whatever you want"
937 http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
938 {{/code}}
939
940 The advantage of the second approach is that curl will take care of url-encode your content, while if you send a file you are responsible for this.
941
942 === Remarks:
943
944 * In the ##application/x-www-form-urlencoded## format the "property#" is a standard immutable prefix that is used to distinguish attributes referring to property values from the attributes referring to the object. For example if we had ##className=XYZ&Text=FOO## we would have had an ambiguity on ##className## because we couldn't understand if ##className## is a property of the object to be set to XYZ or an attribute that describes the object itself (i.e., its metadata like the ##className##). By having the ##property~### prefix this ambiguity is resolved.
945
946 * The information you get back when you retrieve an object (i.e., all
947 the ##<attribute>## elements) are useful when clients need to understand the type of data contained in an object (e.g., when they want to display it). They are not necessary when creating an object because the system already has this information. That's why the XML to be sent is smaller. Actually the only information needed is the ##<className>## and a set of ##<property name="..."><value>## elements.
948
949 * How do you know what kind of information you can send with the XML? You can discover it by using the class description URI. If you go to ##http://localhost:8080/xwiki/rest/wikis/xwiki/classes ## you will get a list of all the classes defined in the Wiki. By looking at this you will understand what are the properties defined by each class, their types and attributes. In that way you will know what you're allowed to put in the ##<property><value>## elements of the XML you send.

Get Connected