Sometimes using the upload applet or other GUI-based tools to get your data into XNAT is not particularly practical. This article describes how to upload a zipped DICOM session directly to XNAT from the bash command line.

Step-by-step guide

You must have a command-line HTTP client tool, such as curl or XnatDataClient, available for this to work.

To upload a Zip file to XNAT, you can POST the file to the /data/services/import REST endpoint, with the upload file specified using the image_archive query variable and multi-part form data encoding. The basic form of this command using curl would be:

curl -u admin:admin --form image_archive=@<file>.zip http://<url>/data/services/import

This makes it pretty easy to do a bulk import. For example, suppose you want to upload all Zip files in a particular folder using this curl command. The following sample script takes the site URL, destination project ID, username and password, and the folder containing the files. The results of each transfer operation are written into a log file named according to the uploaded Zip file, with the log extension added on.

for FILE in ${FOLDER}/*.zip; do
    echo Uploading file ${FILE}
    # To keep data in the prearchive, you can the following parameters to the curl command: --form prearchive_code=0 --form auto-archive=FALSE  
    echo Return status: $(curl -u ${USER}:${PASS} -o ${FILE}.log -w "%{http_code}" --form project=${PROJECT_ID} --form image_archive=@${FILE} "${SITE}/data/services/import?format=html")

There are lots of different ways to get information out of curl to get better insight into what happened during the transfer operation. The sample above just returns the resulting HTTP status code.