Binary upload operation

79 views
Skip to first unread message

Michael Prinz

unread,
Aug 3, 2023, 8:18:23 AM8/3/23
to HAPI FHIR
Hello,

Im am trying to figure out how I can do a client call to a binary upload service at https://<serverbase>/Binary/$upload via POST. The binary is referenced by a file path in the body. No parameters are needed for the call. The body of the call should contain the reference to the file, i.e.:

body: {
  "mode": "file",
  "file": {
     "src": "test.pdf"
   }
}

So far I found out how to call the extended operation:

private static IGenericClient clientGeneric;

MethodOutcome out = clientGeneric.operation()

.onType (Binary.class)

.named ("upload")

.withNoParameters (Parameters.class)

.returnMethodOutcome()

.execute();


But I did not find a way to pass the reference to the file. There is no way to define the body of the POST operation.


Does anyone have an idea?


Thanks,
Mike



Blessed Tabvirwa

unread,
Aug 4, 2023, 3:09:21 AM8/4/23
to HAPI FHIR
Hi Mike

Looking at the patch example I was wondering if using .withBody() could work? 

...
        // Create the body of the POST request
        String body = "{" +
                "  \"mode\": \"file\"," +
                "  \"file\": {" +
                "    \"src\": \"test.pdf\"" +
                "  }" +
                "}";
...

Then add .withBody(body) 


Michael Prinz

unread,
Aug 4, 2023, 6:14:52 AM8/4/23
to HAPI FHIR
Hello blessedndoro2014,

thanks for your suggestion. As far as I understand the patch operation, it is used to patch a resource on the server. You need to identify the resource by providing an identifier. I need to use the extended operation $upload on a Binary resource without any identifier: https://<serverbase>/Binary/$upload. You are right, if I could define or overwrite the body in the POST request ... But I did not find any method to do this on an extended operation i.e. starting with


MethodOutcome out = clientGeneric.operation()

.onType (Binary.class)

.named ("upload")

...

Blessed Tabvirwa

unread,
Aug 4, 2023, 8:39:11 AM8/4/23
to Michael Prinz, HAPI FHIR
i feared as much, how about using parameters? You mentioned "No parameters are needed for the call" , but I was just wondering, could you use withParameters(params) instead of withNoParameters() ?

Where params is defined as follows:

...
       // Create the parameters for the POST request
        Parameters params = new Parameters();
        params.addParameter("mode", "file");
        params.addParameter("file", new Parameters.Parameter().setValue("test.pdf"));
...


--
You received this message because you are subscribed to a topic in the Google Groups "HAPI FHIR" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hapi-fhir/UTveVC148jY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/281d1bbe-1c54-425d-a7d1-d43a6c183413n%40googlegroups.com.


--

Blessed Tabvirwa

HealthIT Developer, #PHASA2018 Ambassador

BSc Computer Science, Certified Azure Architect,
MCSE Data Mgt & Analytics, MCSA, MCPD, FHIR



Michael Prinz

unread,
Aug 4, 2023, 9:59:23 AM8/4/23
to HAPI FHIR
Unfortunately this does not work either. By using the Apache HttpClient-Api instead of hapi-fhir I managed to upload the file successfully. With HttpClient a FileBody can be attached to the request message.

// Define your FHIR server URL

String baseUrl = "https://.../Binary";

String operationName = "upload";


// Construct the complete URL for the custom operation

String operationUrl = baseUrl + "/$" + operationName;


// Create the HTTP POST request

HttpPost httpPost = new HttpPost(operationUrl);

httpPost.setHeader("Content-Type", "application/pdf"); // Set the appropriate content type


FileBody fileBody = new FileBody (new File ("test.pdf"));

MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE ).addPart("file", fileBody);

HttpEntity entity = entityBuilder.build();

httpPost.setEntity(entity);


// Create an HttpClient instance and execute the request

HttpClient httpClient = HttpClients.createDefault();

HttpResponse response = httpClient.execute(httpPost);


But I would rather stick with the hapi-fhir-api.


Reply all
Reply to author
Forward
0 new messages