On Feb 17, 2010, at 12:34 PM, Fabio Mancinelli wrote:
On Feb 10, 2010, at 1:00 AM, Radek Rekas wrote:
I'm having trouble finding out how to upload
attachments using the RESTful api.
I've looked at the attachment type in the schema
(
http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-rest/src/main/…)
but I cant find any mention of how to link the actual attachment data.
Also none of the xml resource representations are loading, I keep getting 404 errors:
http://www.xwiki.org/rel/attachmentDatahttp://www.xwiki.org/rel/attachments
Can anyone provide any tips on how to upload an attachment to a page using the restful
api?
Thanks,
Hi Radek,
in order to upload an attachment to a page wiki:space.page you must do an HTTP PUT on the
http://server/xwiki/rest/wikis/{wiki}/spaces/{space}/pages/{page}/attachmen…
with the body of the request containing the attachment data.
The attached file in the page will have the name you specify in the {attachmentName} part
of your URI.
For example, using curl, you can upload a file main.pdf to the Main.WebHome using the
following command:
$ curl -u Admin:admin -T path_to/main.pdf
http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome/atta…
If you open the Main.WebHome page you will see that a file foo.pdf is attached to the
page and the content is that of your original main.pdf.
The request authenticates you as Admin.
The response, if the upload is successful, is a 201 - CREATED response whose body is the
XML described in the REST XSD's model containing all the information about the newly
created attachment. If the attachment already exists then a 202 - ACCEPTED response code
is returned. The response's body contains the same type of XML.
To complete the picture, there is actually another way to upload attachment
(useful if you are building an application inside XWiki)
To upload an attachment you can POST a multi-part application/form-url-encoded (i.e., what
is usually sent when you put an <input type="file"> element in a form) to
the
http://server/xwiki/rest/wikis/{wiki}/spaces/{space}/pages/{page}/attachmen… URI.
In the form data you can optionally send a "filename" field that contains the
name the attachment will have once uploaded. If this field is not present, then the
original file name is used.
This method can be used when you want to upload files using HTML forms.
Authentication is done either by using HTTP headers (like in the previous example) or, if
they are not present, by using a pre-existing authenticated cookie. That means that if you
display an upload form that points to the previous URI in a wiki page the request will be
sent on behalf of the user that is currently logged in.
-Fabio