The REST API is currently accessible on XNAT Central, http://central.xnat.org. It is advised that you become familiar with the XNAT web application before attempting to use the REST API.
All restful services require a valid user account. This can either be specified via the HTTP Authorization property (using Basic Authorization) or can be performed in an existing Server session (using the JSESSION_ID cookie). As a shortcut, you can use the XNATRestClient to execute REST interactions from a command prompt: xnat_tools.zip (from XNAT 1.4)
A quick is tutorial available at the bottom.
Using 'format' query string parameter to tailor GET results
When querying a resource (GET) which returns a list of items (projects, subjects, experiments, etc), you can use a query string variable 'format=html' to specify the return type of the result. If you are using a REST compliant client, you can also do this using the 'Accept' HTTP header. However, the 'format' tag can override the Accept header, and is especially useful in browsers.
There are currently 4 supported list types: html, json, xml or csv. Specifying any of these values with the format tag, will return a result of that type.
When querying indvidual resources, the xnat xml document will be returned (except when referencing uploaded files).
Using POST or PUT to create xml resources
Creating projects, subjects, experiments, scans, reconstructions and assessments is simple using the REST API. Simply perform a POST or PUT to the relevant API, and your resource will be generated.
You can use several routes to create a resource via the REST API.
POST or PUT an xml document to the relevant URI. This allows you to easily upload chunks of xml documents or an entire document. The xml should be written directly to the body of the HTTP message (not as a form parameter).
POST or PUT an empty HTTP message to the relevant URI. If it is obvious from the URI, what you are creating, then posting an empty document is sufficient to create the item. You can configure additional parameters of the item by adding xml path parameters to the query string. If the type of document to create for the resource is not obvious from the URI (experiments), then you'll need to include at least one xml path parameter, so that the type can be derived.
You can use the XNATRestClient to perform your PUT or POST.
EXAMPLE: Get a list of the subjects in a project:
Create a subject:
This will create an empty subject within your project.
To customize the demographics of the subject you can either add query string parameters or submit an XNAT xml document.
You can modify your entry via the query string by adding xml path parameters to identify the entries you want to edit. You can identify the xml path by review the xnat.xsd.
To modify the gender and handedness of this subject, I'll append the following paths separated by a &. Keep in mind that the querystring parameters must be seperated from the actual URI by a ?.
For your convenience, a few parameters have been added as shortcuts. The abbreviated term can be substituted in the querystring for the full XML PATH. The list of available shortcuts is here.
Instead of specifying each field in the query string, you can build an xml file describing your subject. This can be uploaded with your PUT to set the details of the subject.
Using PUT to modify xml resources
Similar to create resources, you can modify existing resources using the HTTP PUT method on the relevant URI. You can use either a query string xml path variable to specify your modification, or a full xml file.
Notice that, by default, submitting your PUT command will not delete content which is absent from your submitted message. This allows you to submit 'partial' xml documents. If you want to delete the information which is not contained in the document, add the query parameter 'allowDataDeletion=true'.
Using DELETE to remove xml resources
Similarly to creating and modifying, you can use delete existing resources using the HTTP DELETE method on the relevant URI.
EXAMPLE: To delete a subject
By default, any files which have been uploaded for the entry, will not be deleted from the file system. To force the deletion of these files include the removeFiles attribute as a query string parameter.
Files can be uploaded at any of the following levels:
To upload a file, you will need to specify the pertinent URI above and then add the file name. The file name can include a relative directory structure.
So if I have three files which I want to attach to a scan, I could use the following XNATRestClient commands:
I could upload these files using the following commands:
NOTE: If you are using a different utility to upload your files to the REST API, the default expectation of XNAT is that the files will be posted as part of a Multi-Part Form data (similar to a post from within an HTML application). If your tool writes the contents of the file directly into the message body, use the ?inbody=true parameter to force XNAT to parse the contents of the body as the file itself. Using this option requires that you specify what your file should be called in the URI.
Grouping files together
To group files together you will need to define a resource, which you can map multiple files to. Instead of uploading a file to a specific resource uri (URI/files) you can define a resource first. A resource can contain one to many files. This especially useful in uploading file formats which contain multiple files (header and image) or collections of files which you want to be bundled together to represent a single resource.
As an example, if you want to upload an analyze reconstruction you would first create a resource for the reconstruction, then upload files to that resource.
These files would then be retrievable:
You can request a list of files for a given resource using the GET method on the above URIs. To get the list of files for the scan images I uploaded previously I could use:
This would return a list of the three files I previously uploaded.
I could then retrieve each file the URIs specified in the listing OR the URI I used to insert the file originally.