Thank you both very much! Your suggestions worked perfectly... aaron On Mon, Nov 7, 2011 at 8:25 AM, Fabio Mancinelli <[email protected]> wrote:
Hi,
you were almost there :)
Your request has two things that were wrong:
1) A text/plain content type instead of application/xml 2) The URI for POSTing new object is /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects (and not /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects/{className})
...And your XML is missing the className element which will tell the REST API what class has to be used.
So to sum up, 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 (like in your example) So you have to do the following:
$ curl -u Admin:admin -X POST -H "Content-type: application/xml" -H "Accept: application/xml" -d "@test.xml" http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
where test.xml is:
<object xmlns="http://www.xwiki.org"> <className>XWiki.TestClass</className> <property name="Text"> <value>Whatever you want to put here</value> </property> </object>
Alternatively you can use the less verbose application/x-www-form-urlencoded format:
$ curl -u Admin:admin -X POST -H "Content-type: application/x-www-form-urlencoded" -H "Accept: application/xml" -d "@test.txt" http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
where test.txt contains something like: "className=XWiki.TestClass&property#Test=Whatever+you+want".
Or, better, you can use directly curl to specify these parameters using multiple -d switches:
$ curl -u Admin:admin -X POST -H "Content-type: application/x-www-form-urlencoded" -H "Accept: application/xml" -d "className=XWiki.TestClass" -d "property#Test=Whatever you want" http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
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.
Now some remarks:
1) In the application/x-www-form-urlencoded format the "property#" is a standard immutable prefix that is used to distinguish attributes referring to properties 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.
2) The information you get back when you retrieve an object (i.e., all 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.
3) 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.
Hope this answers your questions.
-Fabio
On Sat, Nov 5, 2011 at 9:46 AM, Ashtar Communications <[email protected]> wrote:
Hi,
Is it possible to create a new object of a custom class using the RESTful API?
The documentation page only lists the following POST method, which doesn't accept a class name: /wikis/{wikiName}/spaces/{spaceName}/pages/{pageName}/objects[?start=offset&number=n]
I have tried things like the following: curl -u Admin:admin -X POST -H "Content-type: text/plain" -H "Accept: application/xml" -d "@test.xml" http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects/XWiki...
Where test.xml contains some variation on: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <properties xmlns="http://www.xwiki.org"> <property type="com.xpn.xwiki.objects.classes.TextAreaClass" name="Text"> <attribute value="Text" name="name"/> <attribute value="Test" name="prettyName"/> <attribute value="0" name="unmodifiable"/> <attribute value="0" name="disabled"/> <attribute value="100" name="size"/> <attribute value="10" name="rows"/> <attribute value="" name="customDisplay"/> <attribute value="Text" name="editor"/> <attribute value="6" name="number"/> <attribute value="0" name="picker"/> <attribute value="" name="validationMessage"/> <attribute value="" name="validationRegExp"/> <value> This is some text. </value> </properties>
Curl returns this: <html> <head> <title>Status page</title> </head> <body> <h3>The method specified in the request is not allowed for the resource identifi ed by the request URI</h3><p>You can get technical details <a href="http://www.w 3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6">here</a>.<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html>
Simpler syntax without the XML file still fails for me. In fact, I can't seem to even get the basic "create object" method to work, even without the class. For example: curl -u Admin:admin -X POST http://localhost/xwiki/rest/wikis/xwiki/spaces/Test/pages/Test/objects
Just comes back with a "malformed syntax" error.
Is there an example anywhere of how to do this?
Thank you,
Aaron _______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users