How to use REST API to PUT experiments?

296 views
Skip to first unread message

Jianliang Gao

unread,
Feb 26, 2018, 9:51:07 AM2/26/18
to xnat_discussion
Dear All,

I am learning XNAT REST API to create project, subject and etc... 
I can create project and subject, but I am stuck with the experiments and onwards.

for example, I used

./XNATRestClient -host http://10.1.1.17 -u USERNAME -p PASSWD -m PUT -remote "/data/archive/projects/datarelease1_2/subjects/CC00099AN18/experiments/MR_session"

I got the error as

<html>
<head>
   <title>Status page</title>
</head>
<body>
<h3>Need PUT Contents</h3><p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.18">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>


I searched the wiki but failed to find helpful docs. Can you please help me out of this? Many thanks.

Best wishes,
Jianliang

McKay, Mike

unread,
Feb 26, 2018, 10:26:47 AM2/26/18
to xnat_discussion

I would try adding ?xsiType=xnat:mrSessionData to the end of the URL. So it looks like: "/data/archive/projects/datarelease1_2/subjects/CC00099AN18/experiments/MR_session?xsiType=xnat:mrSessionData" or "/data/archive/projects/datarelease1_2/subjects/CC00099AN18/experiments/myMrSessionId?xsiType=xnat:mrSessionData" with whatever session ID you want.


-Mike


From: xnat_di...@googlegroups.com <xnat_di...@googlegroups.com> on behalf of Jianliang Gao <pilla...@gmail.com>
Sent: Monday, February 26, 2018 8:51:07 AM
To: xnat_discussion
Subject: [XNAT Discussion] How to use REST API to PUT experiments?
 
--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xnat_discussi...@googlegroups.com.
To post to this group, send email to xnat_di...@googlegroups.com.
Visit this group at https://groups.google.com/group/xnat_discussion.
For more options, visit https://groups.google.com/d/optout.

 


The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

Herrick, Rick

unread,
Feb 26, 2018, 10:31:07 AM2/26/18
to xnat_di...@googlegroups.com

You need to provide a description of the experiment for the PUT call in the form of an XML document. XNAT can create a project or subject with just the information provided on the REST path:

 

http://10.1.1.17/data/projects/PROJ_ID

http://10.1.1.17/data/projects/PROJ_ID/subjects/SUBJ_ID

 

The first one creates a project with the ID PROJ_ID, the second creates a subject in project PROJ_ID with the label SUBJ_ID. The issue with experiments is that there are many different types of experiment to create, along with a number of properties that can be set. The easiest way to create a new experiment is PUT an XML document to the REST call. You can do this with a really basic XML descriptor. For your example below the following XML would work:

 

<xnat:MRSession ID="" label="MR_session" xmlns:xnat="http://nrg.wustl.edu/xnat"/>

 

You must provide that blank ID attribute, since the XML will not parse without it. For XNATRestClient, you just need to save that to a file, e.g. MR_session.xml, and specify that with the -local parameter.

 

You can get a lot more complicated with the XML you send to the server, including creating scans, etc., for imaging sessions or other property values for different types of subject or image assessors, but this will get you started. You can get templates to work with by downloading different experiment types from the server. On the page for an existing experiment in the UI, you click on Download XML and use that as a template, or download through REST by doing a GET with “format=xml” added to the query string (I use httpie instead of XNATRestClient, which is no longer supported):

 

$ http --session=admin xnatdev.xnat.org/data/projects/XNAT_01/subjects/XNAT_01_01/experiments/XNAT_E00008 format==xml

HTTP/1.1 200 OK

Accept-Ranges: bytes

Connection: keep-alive

Content-Security-Policy: frame-ancestors 'self'

Content-Type: text/xml

Date: Mon, 26 Feb 2018 15:30:08 GMT

Server: nginx/1.10.3 (Ubuntu)

Set-Cookie: SESSION_EXPIRATION_TIME="1519659008810,900000"; Version=1; Path=/

Transfer-Encoding: chunked

Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xnat:MRSession ID="XNAT_E00008" project="XNAT_01" label="XNAT_01_01_MR08" xmlns:arc="http://nrg.wustl.edu/arc" xmlns:val="http://nrg.wustl.edu/val" xmlns:pipe="http://nrg.wustl.edu/pipe" xmlns:wrk="http://nrg.wustl.edu/workflow" xmlns:scr="http://nrg.wustl.edu/scr" xmlns:xdat="http://nrg.wustl.edu/security" xmlns:cat="http://nrg.wustl.edu/catalog" xmlns:prov="http://www.nbirn.net/prov" xmlns:xnat="http://nrg.wustl.edu/xnat" xmlns:xnat_a="http://nrg.wustl.edu/xnat_assessments" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nrg.wustl.edu/workflow http://xnatdev.xnat.org/schemas/workflow.xsd http://nrg.wustl.edu/catalog http://xnatdev.xnat.org/schemas/catalog.xsd http://nrg.wustl.edu/pipe http://xnatdev.xnat.org/schemas/repository.xsd http://nrg.wustl.edu/scr http://xnatdev.xnat.org/schemas/screeningAssessment.xsd http://nrg.wustl.edu/arc http://xnatdev.xnat.org/schemas/project.xsd http://nrg.wustl.edu/val http://xnatdev.xnat.org/schemas/protocolValidation.xsd http://nrg.wustl.edu/xnat http://xnatdev.xnat.org/schemas/xnat.xsd http://nrg.wustl.edu/xnat_assessments http://xnatdev.xnat.org/schemas/assessments.xsd http://www.nbirn.net/prov http://xnatdev.xnat.org/schemas/birnprov.xsd http://nrg.wustl.edu/security http://xnatdev.xnat.org/schemas/security.xsd">

<xnat:subject_ID>XNAT_S00001</xnat:subject_ID>

</xnat:MRSession>

 

HTH.

 

-- 

Rick Herrick

Sr. Programmer/Analyst

Neuroinformatics Research Group

Washington University School of Medicine

Phone: +1 (314) 273-1645

From: "xnat_di...@googlegroups.com" <xnat_di...@googlegroups.com> on behalf of Jianliang Gao <pilla...@gmail.com>
Reply-To: "xnat_di...@googlegroups.com" <xnat_di...@googlegroups.com>
Date: Monday, February 26, 2018 at 8:51 AM
To: "xnat_di...@googlegroups.com" <xnat_di...@googlegroups.com>
Subject: [XNAT Discussion] How to use REST API to PUT experiments?

 

--

You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to


To post to this group, send email to

Jianliang Gao

unread,
Feb 27, 2018, 4:15:46 AM2/27/18
to xnat_discussion
Hi Mike and Rick,

Thank you very much for your help. I practised Mike's suggestion and yes, I did insert session data successfully. Then I checked my project and subject information and compared to the original metadata from an existing XNAT dataset. I missed some information such as gender, date etc. Here I enclose the xml file of the subject metadata I downloaded from an existing XNAT database using GET method. To be honest I don't quite get Rick's explanation in the second part regarding httpie. The example you demo is equivalent to GET, isn't it?  Do you mind to show me how I can PUT the xml to my XNAT database with both methods, XNATRestClient and httpie?

Thank you very much.

Best wishes,
Jianliang
CC00099AN18.xml

Herrick, Rick

unread,
Feb 27, 2018, 3:32:11 PM2/27/18
to xnat_di...@googlegroups.com

The example with “http xnatdev.xnat.org/data/experiments/XNAT_E00008 format==xml” is a GET operation, if that’s what you referring to. That is just a way to get the XML for an existing data object in XNAT. Note that, if you use the experiment ID, like XNAT_E12345, you don’t need the project and subject, since that ID is unique across the system. In the XML you sent, the subject ID is CC00099AN18, so you could retrieve that with something like (the --download flag just tells httpie to write the response body to a file instead of displaying on stdout):

 

$ http --download xnatdev.xnat.org/data/experiments/XNAT_E00008 format==xml

 

I took the subject XML you attached to your last post and edited it down to what I’ve attached here. I then sent that to the server:

 

$ http --session=admin PUT xnatdev.xnat.org/data/projects/XNAT_01/subjects/XNAT_01_02 < CC00099AN18.xml

HTTP/1.1 201 Created

Accept-Ranges: bytes

Connection: keep-alive

Content-Length: 11

Content-Security-Policy: frame-ancestors 'self'

Content-Type: text/plain;charset=ISO-8859-1

Date: Tue, 27 Feb 2018 20:29:19 GMT

Server: nginx/1.10.3 (Ubuntu)

Set-Cookie: JSESSIONID=22531260E66381E5BD3EC404A580598A; Path=/; HttpOnly

Set-Cookie: SESSION_EXPIRATION_TIME="1519763357724,900000"; Version=1; Path=/

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

 

XNAT_S00003

 

You’ll need to remove that <experiments> element in the subject you pulled down: the experiments won’t be created as part of the subject creation and won’t resolve properly. But otherwise you can specify just about any other property or attribute you like through the XML.

 

-- 

Rick Herrick

Sr. Programmer/Analyst

Neuroinformatics Research Group

Washington University School of Medicine

Phone: +1 (314) 273-1645

Subject: Re: [XNAT Discussion] How to use REST API to PUT experiments?

Hi Mike and Rick,

 

Thank you very much for your help. I practised Mike's suggestion and yes, I did insert session data successfully. Then I checked my project and subject information and compared to the original metadata from an existing XNAT dataset. I missed some information such as gender, date etc. Here I enclose the xml file of the subject metadata I downloaded from an existing XNAT database using GET method. To be honest I don't quite get Rick's explanation in the second part regarding httpie. The example you demo is equivalent to GET, isn't it?  Do you mind to show me how I can PUT the xml to my XNAT database with both methods, XNATRestClient and httpie?

 

Thank you very much.

 

Best wishes,

Jianliang

On Monday, 26 February 2018 15:26:47 UTC, Michael McKay wrote:

I would try adding ?xsiType=xnat:mrSessionData to the end of the URL. So it looks like: "/data/archive/projects/datarelease1_2/subjects/CC00099AN18/experiments/MR_session?xsiType=xnat:mrSessionData" or "/data/archive/projects/datarelease1_2/subjects/CC00099AN18/experiments/myMrSessionId?xsiType=xnat:mrSessionData" with whatever session ID you want.

 

-Mike

The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
xnat_discussi...@googlegroups.com.
To post to this group, send email to
xnat_di...@googlegroups.com.
Visit this group at
https://groups.google.com/group/xnat_discussion.
For more options, visit
https://groups.google.com/d/optout.

CC00099AN18.xml

Will Horton

unread,
Feb 27, 2018, 4:38:04 PM2/27/18
to xnat_discussion
Incidentally, some of this is covered in this page of the API documentation: https://wiki.xnat.org/display/XAPI/Upload+Image+Session+Files+with+REST+API

I'll update this page shortly to add the tips mentioned below on uploading XML definitions of experiments and their attributes.

Jianliang Gao

unread,
Mar 6, 2018, 3:32:07 AM3/6/18
to xnat_discussion
Hi Rick,

I have been trying to run http command to retrieve information from one xnat data server
I got errors like
```
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 185
Content-Type: text/html
Date: Mon, 05 Mar 2018 17:29:12 GMT
Server: nginx/1.10.1

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.1</center>
</body>
</html>
```
I also tried your example and got errors like
```
http: error: ConnectionError: HTTPConnectionPool(host='xnatdev.xnat.org', port=80): Max retries exceeded with url: /data/projects/XNAT_01/subjects/XNAT_01_01/experiments/XNAT_E00008?format=xml (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f4195282f90>: Failed to establish a new connection: [Errno 113] No route to host',))
```

Anything I missed?

Best wishes,
Jianliang

To unsubscribe from this group and stop receiving emails from it, send an email to xnat_discussion+unsubscribe@googlegroups.com.
To post to this group, send email to
xnat_discussion@googlegroups.com.
Visit this group at
https://groups.google.com/group/xnat_discussion.
For more options, visit
https://groups.google.com/d/optout.

Herrick, Rick

unread,
Mar 6, 2018, 12:21:10 PM3/6/18
to xnat_di...@googlegroups.com

The issue with the 301 is that the site is redirecting you to the https version of the site. You have two options:

 

Add the --follow flag to your httpie call:

 

http --follow http://data.developingconnectome.org/data/archive/projects/DataRelease1_1/subjects/CC00099AN18 format=xml

 

Or change the URL directly to https:

 

http https://data.developingconnectome.org/data/archive/projects/DataRelease1_1/subjects/CC00099AN18 format=xml

 

By default, httpie, just like curl, doesn’t follow 30x redirects from HTTP servers, just displays them. Here’s the curl equivalent:

 

$ curl http://data.developingconnectome.org/data/archive/projects/DataRelease1_1/subjects/CC00099AN18

<html>

<head><title>301 Moved Permanently</title></head>

<body bgcolor="white">

<center><h1>301 Moved Permanently</h1></center>

<hr><center>nginx/1.10.1</center>

</body>

</html>

 

Also, I’m pretty certain that you don’t want format=xml on that command, but format==xml. Httpie interprets x=y as a data field to be included in the request body, by default as JSON but can be as a form if the --form parameter is specified. With your version, httpie does this (a lot of extraneous header info removed from the output):

 

$ http --verbose --session=admin xnatdev.xnat.org/data/projects/XNAT_01/subjects/XNAT_01_01 format=xml

POST /data/projects/XNAT_01/subjects/XNAT_01_01 HTTP/1.1

Accept: application/json, */*

 

{

    "format": "xml"

}

 

HTTP/1.1 405 Method Not Allowed

Allow: DELETE, PUT, GET, OPTIONS, HEAD

 

This is really powerful feature and one of the main reasons I use it, but you need to be aware of what it’s doing or you’ll end up getting something other than what you expect! If you change that ‘=’ to ‘==’, you’ll get this:

 

$ http --verbose --session=admin xnatdev.xnat.org/data/projects/XNAT_01/subjects/XNAT_01_01 format==xml

GET /data/projects/XNAT_01/subjects/XNAT_01_01?format=xml HTTP/1.1

 

HTTP/1.1 200 OK

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<xnat:Subject ID="XNAT_S00001" project="XNAT_01" label="XNAT_01_01" …>

    …

</xnat:Subject>

 

You’ll also need to authenticate against your server. I tried your call with format==xml and got this:

 

$ http https://data.developingconnectome.org/data/archive/projects/DataRelease1_1/subjects/CC00099AN18 format==xml

HTTP/1.1 401 Unauthorized

 

Which makes sense if it’s not an open public XNAT instance. Authenticating and accessing with httpie is pretty simple. For one call do this:

 

$ http --auth owner --session=owner xnatdev.xnat.org/data/projects

http: password for ow...@xnatdev.xnat.org:

 

Once you’ve authenticated, the session information is cached just like a browser caches it in a cookie and you can reuse the same session with just the --session argument:

 

$ http --session=owner xnatdev.xnat.org/data/projects

 

Regarding your issues trying my examples, that’s because xnatdev.xnat.org doesn’t exist on the internet. I have a Vagrant VM built with the XNAT Vagrant project with the host and server parameters set to xnatdev.xnat.org in the local.yaml, the site URL set to http://xnatdev.xnat.org, and xnatdev.xnat.org configured in my desktop machine’s /etc/hosts file to the default IP for that VM. You can try my examples on your own server by changing the server address (http://xnatdev.xnat.org becomes https://data.developingconnectome.org) and using valid project, subject, session labels in the REST URLs.

Image removed by sender.

Image removed by sender.

The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
xnat_discussi...@googlegroups.com.
To post to this group, send email to
xnat_di...@googlegroups.com.
Visit this group at
https://groups.google.com/group/xnat_discussion.
For more options, visit
https://groups.google.com/d/optout.

 

The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.

To unsubscribe from this group and stop receiving emails from it, send an email to


To post to this group, send email to

Moore, Charlie

unread,
Mar 6, 2018, 12:35:29 PM3/6/18
to xnat_di...@googlegroups.com

Jianliang,

 

Try using https in your URL instead of http.

 

Thanks,

Charlie

The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

--

You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xnat_discussi...@googlegroups.com.
To post to this group, send email to xnat_di...@googlegroups.com.
Visit this group at https://groups.google.com/group/xnat_discussion.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages