Question
· Jul 7, 2023

REST conversion from structure format to JSON

I am playing around with trying to make an Epic REST API call from an operation, and from what I understood because the request has to be sent as POST, I need to send the request as JSON. However when I try taking the request and running %ToJSON against it for the payload to be created I am getting an error...

ERROR <Ens>ErrException: <METHOD NOT SUPPORTED>zgetPatientLocationByVisit+6^User.SCOTT.REST.APIOperation.1 *%ToJSON,osuwmc.Epic.Access.Request.GetPatientLocationByVisit2JsonRequest -- logged as '-' number - @' set tPayload = tRequest.%ToJSON()'

Reading the documentation I tried switching my object to use DynamicAbstractObject instead of DynamicObject as I thought I read where %ToJSON could only be used with DynamicAbstractObject but got this error now...

ERROR <Ens>ErrException: <METHOD DOES NOT EXIST>zgetPatientLocationByVisit+2^User.SCOTT.REST.APIOperation.1 *%New,%Library.DynamicAbstractObject -- logged as '-' number - @' set tRequest = ##class(%Library.DynamicAbstractObject).%New()'

This is the first time I am trying to make REST calls instead of using SOAP.

Below is my Operation, can someone tell me what I might be doing wrong?

 

Class User.SCOTT.REST.HTTPOutboundAdapter Extends (EnsLib.HTTP.OutboundAdapter, EnsLib.REST.Operation)

{

 

/// Send a POST to the configured Server, Port and URL, sending form data to the named form variables.

Method Post(Output pHttpResponse As %Net.HttpResponse, pFormVarNames As %String, pData...) As %Status

{

     quit ..SendFormDataArray(.pHttpResponse, "POST", ..GetRequest(), .pFormVarNames, .pData)

}

 

ClassMethod GetRequest() As %Net.HttpRequest

{

     set request = ##class(%Net.HttpRequest).%New()

     set request.ContentType = "application/json"

     quit request

}

 

}

 

 

Class User.SCOTT.REST.APIOperation Extends User.SCOTT.REST.HTTPOutboundAdapter

{

 

Parameter INVOCATION = "Queue";

 

Method getPatientLocationByVisit(pRequest As osuwmc.Epic.Access.Request.GetPatientLocationByVisit2JsonRequest, Output pResponse As osuwmc.Epic.Access.Response.GetPatientLocationByVisit2JsonResponse) As %Status

{

  try {

    set tRequest = ##class(%DynamicAbstractObject).%New()

    set tRequest.authorization = "Basic"

    set tRequest.clientID = "Epic-Client-ID = xxxxxxxxxxxxxx"

    set tRequest.payload = pRequest

    set tPayload = tRequest.%ToJSON()

 

    Set tSC=..Adapter.Post(.tHttpResponse, , tPayload)

 

    If $$$ISERR(tSC)&&$IsObject(tHttpResponse)&&$IsObject(tHttpResponse.Data)&&tHttpResponse.Data.Size {

    Set tSC=$$$ERROR($$$EnsErrGeneral,$$$StatusDisplayString(tSC)_":"_tHttpResponse.Data.Read())

    }

    Quit:$$$ISERR(tSC)

    if $IsObject(tHttpResponse){

      set pResponse = ##class(osuwmc.Epic.Access.Response.GetPatientLocationByVisit2JsonResponse).%New()

      set tSC = ..JSONStreamToObject(tHttpResponse.Data, .tProxy)

 

      if (tSC){

        set pResponse.AppointmentSchedules = tProxy.AppointmentSchedules

        set pResponse.AttendingPhysicians = tProxy.AttendingPhysicians

      }

    }

  }

  catch{

    set tSC = $$$SystemError

  }

  quit tSC

}

Product version: IRIS 2022.1
Discussion (3)1
Log in or sign up to continue

The error message <METHOD NOT SUPPORTED> says everything.

USER>s obj={"name":"john", "value":65}

USER>w obj.%ToJSON() // --> {"name":"john","value":65}

USER>s obj={"name":"john", "value":65, "dynObj":{"info":"something"} }

USER>w obj.%ToJSON() // --> {"name":"john","value":65,"dynObj":{"info":"something"}}

USER>s obj={"name":"john", "value":65, "dynObj":{"info":"something"}, "cosObj":(##class(%Net.HttpRequest).%New()) }

USER>w obj.%ToJSON() // --> <METHOD NOT SUPPORTED> *%ToJSON,%Net.HttpRequest

So the problem is, you try to "stringify" (JS-speech) a dynamic object, where one of the properties contains a non-dynamic object (%Net.HttpRequest in the above example).

I am not sure I understand... I was using the example that was provided in the documentation.

https://docs.intersystems.com/healthconnectlatest/csp/docbook/DocBook.UI.Page.cls?KEY=EREST_operation#EREST_operation_json

If I have to have a Custom Header do I not have to define in this case the Client ID as part of the %DynamicObject?

    set tRequest = ##class(%DynamicAbstractObject).%New()

    set tRequest.clientID = "Epic-Client-ID = xxxxxxxx"

if I am using a Request Class Structure (osuwmc.Epic.Access.Request.GetPatientLocationByVisit2JsonRequest) isn't the set tRequest.payload = pRequest line correct?