3

I want to extract the HTTP status of a HAPI FHIR create Method.

MethodOutcome outcome = client.create().resource(medicationOrders[0]).prettyPrint().encodedXml().execute();

Is there any way to recover it from the MethodOutcome or any other workaround exists?

2 Answers2

2

There are a few things that can be useful..

If the method returns successfully, then you have gotten an HTTP 2xx response back. There isn't a way to tell if it was a 200 or a 204 for example, but it was a successful response.

If the method throws a BaseServerResponseException of some sort, the server returned a 4xx or 5xx status code. You can call BaseServerResponseException#getStatusCode() to find out which one.

If you need to know the exact response in all cases, you can use a client interceptor to find that.

James Agnew
  • 701
  • 4
  • 3
0

You can obtain status code using Kotlin like this with client interceptors,

  1. Create an interceptor to pick status codes,

     private fun createClientInterceptor(statusCodes: MutableList<Int>): 
         IClientInterceptor {
             return object : IClientInterceptor {
                 override fun interceptRequest(theRequest: IHttpRequest?) {}
                 override fun interceptResponse(theResponse: IHttpResponse?) {
                     if (theResponse != null) {
                         println(theResponse.status)
                     }
                 }
             }
     }
    
  2. Create a client and register the interceptor

     val ctx = FhirContext.forR4()!!
     val restfulGenericClient =  ctx.newRestfulGenericClient(getServerUrl())
     restfulGenericClient.registerInterceptor(createClientInterceptor(statusCodes))
    

In this way, you can collect status codes of responses in Kotlin, appropriately you can change the code to Java as well.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Janitha Madushan
  • 1,453
  • 3
  • 28
  • 40