How to send images to another restAPI?

Hello everyone,

I am trying to use Orthanc as a DICOM server, from Orthanc I want to send all received DICOM files to a restAPI. I am not sure what is the best way to do this. To better explain the problem, every time that Orthanc receives a new DICOM instance it should be posted at my restAPI, if the restAPI return false the DICOM instance should be saved. I’m thinking of two ways to solve this problem:

1 - Developing a Orthanc Plugin which uses the OnStoredCallback function to save the DICOM image at the filesystem instance every time Orthanc receives one. Another function inside OnStoredCallback will loop through all the files at the buffer and will try to send it to the API (The buffer is necessary if some error occurs in the RESTAPI).

2 - Developing a Orthanc Plugin that saves the received instance name from the image in a txt file, and a script that uses Orthanc restAPI to download each DICOM image and send it to my restAPI.

Is there any easier way to do this? Otherwise, what would be the best option?

Hi Lucas,

Yes, there are a few alternatives.

  1. You may write a python script that will monitor the /changes route, you’ll be able to trigger an event on each NewInstance event. Then, your python script can download the file at /instances/{id}/file, push it to your webservice and delete it by sending a DELETE request at /instances/{id}

You might use a Lua script: http://book.orthanc-server.com/users/lua.html.

  1. In OnStoredInstance call back, you can have access to the dicom tags and send them to your webservice with HttpPost(). Unfortunately, HttpPost is not able to send binary content so you can’t use it to POST your complete file to your server.

  2. You might have the same workflow with IncomingFindRequestFilter callback. Again, you’ll have access only to the tags, not the file (because, in that case, it’s not stored in Orthanc yet)

  3. Something a bit tricky that may work for you. (I have never tried it !).
    You might declare your web server as an orthanc peer in the configuration file. Peers are expecting to receive dicom files in POST requests in a route like http://myserver.com/instances.
    Then, in a lua script, you may issue a POST request to /peers/yourserver_alias/store with, in the body, the instance id you wish to forward. Then, you may check the response from your server and delete the instance if it does not fit.

  4. If your web server does not need to look at the pixels data, you might probably do your whole filtering in a Lua script where you have access to all dicom tags.

Best regards,

Alain.

Thank you for the answer !

I have chose the second and the fifth answer, but I still have some questions about it. In case of the fifth alternative, I have to make sure that all instances are being sent to my API even when some problem occurs with the API, so I would like to know if there is some kind of retry in case of problems with the Orthanc peer (My API) . About the second option, I would like to know what happen when a OnStoredInstance callback is called and the last OnStoredInstance callback was not finished, do they line up? I’m asking that because of the same problem, I want to make a loop trying to send the DICOM file to my API, I need to be sure that the others OnStoredCallbacks are being queued.

Best Regards,

Lucas Fernandes

Unfortunately, all lua callbacks will be called only once. There won’t be any kind of retry.
Furthermore, you should avoid to do the retries inside the lua script because these are supposed to be fast (during a lua script execution, many thinks are locked in Orthanc) so if you spend more than 1 second in your lua script, you might see some performance issue.

If you need some kind of retries, I would suggest the 1st option: the python script (or any other language) that monitors the /changes route (http://book.orthanc-server.com/users/rest.html?highlight=changes#tracking-changes). Each change has a unique id. You can then keep track of the changes you have already handled correctly and the ones that you need to retry.

Note: there are plenty of python samples available here: https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Python/. Many of them do monitor the /changes route