Question

I need to create (with XWiki API REST) a BlogPost. But I don't know to use REST... With this command :

$ 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

and with this XML file :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<page xmlns="http://www.xwiki.org">     
        <title>Hello world</title>
        <syntax>xwiki/2.0</syntax>
        <content>This is a new page</content>
</page>

I create a Page in the space Main. But I want a BlogPost in the space Blog. What's the markup that I must use? (I have found this command here)

Was it helpful?

Solution

The right command to use is:

curl -u Admin:admin -X POST --data-binary "@blogpost.xml" -H "Content-Type: application/xml" http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Blog/pages/A+New+Blogpost/objects

The XML file should look like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<object xmlns="http://www.xwiki.org"><className>Blog.BlogPostClass</className>
    <property name="category"><value>Blog.News</value></property>
    <property name="content"><value>This is the new blog post. You can use **wiki syntax** in it.

Don't forget to escape XML entities like &amp; and &lt;!</value></property>
    <property name="publishDate"><value>2012-06-06 12:00:00.0</value></property>
    <property name="published"><value>1</value></property>
    <property name="title"><value>Second blog post</value></property>
</object>

A few things to note:

  • The document must already exist, this doesn't work for a non-existing document. So, first thing to do is to PUT a title and parent into a new document, then you can add objects to it.
  • The right verb for creating objects is POST, and the URL is obtained by appending /objects to the document's REST URL.
  • The actual REST URL of the newly created object is returned as a redirect. You can either intercept the redirect and process the URL as you need, or you can let the redirect go through and you'll get back the object that was created with the verbose full syntax.
  • In general, when sending resources the same syntax as the one returned when getting resources is used, although not all the data is needed. For instance, I obtained the XML for this object by first getting an existing blogpost object, then removing all that the server doesn't need to receive since it can compute it locally.
  • One thing I noticed (and I consider a bug) is that when getting resources, XML entities are encoded twice (&amp;#39;), while sent resources should have entities encoded only once (&#39;).

OTHER TIPS

Generally with REST, you'd expect to send the PUT message to the URL that you wish to create (or update). Thus, you'd change from:

http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/NewPage

to maybe this (assuming you're creating BlogPost in the space Blog):

http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Blog/pages/BlogPost

The rest of the command should be whatever works for you. Since it's working now for creating a page in the Main space, I'd expect it to be enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top