How to send OrthancPlugins::RestApiPost with form-data ?

352 views
Skip to first unread message

Christopher

unread,
Dec 2, 2021, 11:08:02 PM12/2/21
to Orthanc Users
Hi Authors,
I am developing plugin in C++ which using internal OrthancPlugins::RestApiPost. What I want to achieve is to send multiple dicom instances via route "/instances" to internal API. But I dont find any signature from OrthancPlugins::RestApiPost to send form-data. 

This is the code I tried, but orthanc denied 

std::vector<std::string> paths = {"00001.dcm", "00002.dcm"};
for (const auto& path : paths) {
std::string dicom;
Orthanc::SystemToolbox::ReadFile(dicom, path);
formData.append(dicom);
}
Json::Value upload;
OrthancPlugins::RestApiPost(upload, "/instances", formData, false);

I1203 10:53:27.944115 OrthancPlugins.cpp:2755] (plugins) Plugin making REST POST call on URI /instances (built-in API) I1203 10:53:27.944187 OrthancRestApi.cpp:173] (http) Receiving a DICOM file of 380876717 bytes through HTTP E: DcmElement: Unknown Tag & Data (225b,755c) larger (808464432) than remaining bytes (380876709) in file, premature end of stream E1203 10:53:28.146568 OrthancException.cpp:57] Bad file format: Cannot parse an invalid DICOM file (size: 380876717 bytes) E1203 10:53:28.159781 PluginsManager.cpp:197] Exception while invoking plugin service 3002: Bad file format E1203 10:53:28.182317 PluginsErrorDictionary.cpp:111] Exception inside the plugin engine: Bad file format

Can you please help what is the right way ? Thanks

Sébastien Jodogne

unread,
Dec 4, 2021, 4:52:32 AM12/4/21
to Orthanc Users
Hello,

POST on "/instances" only accepts one DICOM instance or one ZIP file at once. You should use a code like the following (untested):

    std::vector<std::string> paths = {"00001.dcm", "00002.dcm"};
    for (const auto& path : paths) {
      std::string dicom;
      Orthanc::SystemToolbox::ReadFile(dicom, path);
      Json::Value upload;
      OrthancPlugins::RestApiPost(upload, "/instances", dicom, false);
    }    

If you absolutely want to upload multiple DICOM instances in one single call, and if you don't want to create a ZIP file, you could use a STOW-RS call through the DICOMweb plugin:

HTH,
Sébastien-

Christopher

unread,
Dec 4, 2021, 6:56:12 AM12/4/21
to Orthanc Users
Thanks Sebastien,

I notice that if I am using PostMan (or curl) to send multiple dicom files (form data) to the http route: localhost:8042/instances, then orthanc can handle them properly. However if I am using c++ sdk then the OrthancPlugins::RestApiPost does not have signature to send the form-data. What  I want to achieve is to  digest multiple files at once instead of sending one by one. 

Sébastien Jodogne

unread,
Dec 4, 2021, 8:05:58 AM12/4/21
to Orthanc Users
Indeed, the "/instances" URI also accepts "multipart/form-data" requests, but this is normally only meant to be used by HTML/JavaScript applications or HTTP clients (such as postman or curl). Plugins or REST clients should favor DICOMweb STOW-RS and ZIP uploads (cf. my previous message).

That being said, if you absolutely want to use "/instances" with "multipart/form-data", check out RFC1867 that contains samples about how to properly format the headers and the body of a suitable POST request:

You should then be able to use "OrthancPlugins::RestApiPost()".

HTH,
Sébastien-

Christopher

unread,
Dec 4, 2021, 11:51:49 AM12/4/21
to Orthanc Users
Great, thank you for your lots of useful information. 
Reply all
Reply to author
Forward
0 new messages