Nethanjalie Lelwala
Nethanjalie Lelwala

Reputation: 77

Constructing POST request message for MEdicationOrder Resource HAPI - FHIR

I am writing a java code for generating a POST request using MedicationOrder resource in HAPI - FHIR DSTU2 HL7. I have come across several troubles.

  1. Setting the reference values for the contained resources.
  2. Contained resources are not present in generated XML message.
  3. Operation outcome is HTTP/1.1 500 Internal Server Error with the message Expecting outer element called 'feed', found: MedicationOrder.

Can anyone familiar with MedicationOrder resource please help me?

below is the java code

public int sendMessage(MedicationOrder medicationOrder) throws   ClientProtocolException, IOException
{
  FhirContext ctx = FhirContext.forDstu2Hl7Org();
  IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu2");    
  HttpPost httpPost = new HttpPost("http://fhirtest.uhn.ca/baseDstu2");

  String message = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(medicationOrder);
  httpPost.setEntity((HttpEntity) new StringEntity(message, ContentType.create(Constants.CT_FHIR_XML, "UTF-8")));

  org.apache.http.HttpResponse response = client.getHttpClient().execute(httpPost);
  return response.getStatusLine().getStatusCode();
}

Upvotes: 0

Views: 903

Answers (2)

James Agnew
James Agnew

Reputation: 701

It looks like you're mixing up HAPI's client with the Apache HTTP client layer (which is internal to HAPI's client, but you don't need in interact with directly).

Instead of creating the HttpPost object, just use HAPI's client to perform the create:

MethodOutcome outcome = client.create()
  .resource(medicationOrder)
  .prettyPrint()
  .encodedJson()
  .execute();

Upvotes: 0

Lloyd McKenzie
Lloyd McKenzie

Reputation: 6803

If the interface is complaining about "feed", then it sounds like you're using the DSTU 1 version of HAPI, not DSTU2. (Feed was changed to Bundle in DSTU 2.)

Upvotes: 0

Related Questions