· Oct 10, 2023

Question how to get JSON-response back to web-service/client

Hello everyone!
I have manually created a REST-service that receives incoming HTTP-GET calls together with an URL-map. As shown below:

XData UrlMap [ XMLNamespace = "]
<Route Url="/testGet" Method="GET" Call="Handler" />

ClassMethod Handler(req As %Stream.Object) As %Status
set Class = ##class(Ens.Request).%New()

set status = ##class(Ens.Director).CreateBusinessService("TestService", .instance)
set status = instance.OnProcessInput(Class, .response)

if $ISOBJECT(response)
         write response.%ToJSON()
} Quit $$$OK


Through the code above I am utilizing a service that I have created as well, as shown here:

Method OnProcessInput(pInput As Ens.Request, Output pOutput As %RegisteredObject) As %Status
$$$LOGINFO("inside service")

set req = ##class(Ens.Request).%New()
set tsc = ..SendRequestSync(..TargetConfigName, req, .tResponse)
set pOutput = tResponse

Quit tsc

With this code I send a request to another Operation that I have created: 

Method PostMessage(pRequest As Ens.Request, Output pResponse As %RegisteredObject) As %Status
set pResponse = {"responseText":"Hello world!"}
return pResponse
Quit $$$OK

Now, my real question is how can I get the pResponse which is {"responseText":"Hello world!"} to get returned back to my webservice and printed out in Postman? I have tried the principle that is followed here: Creating a RESTful Service using Ensemble | InterSystems Developer , and that worked perfectly fine. But I cannot make it work if I take it a step further and try to retrieve the response from the Operation. In Postman I receive a 200 OK, but in the service this error is logged: 

ERROR <Ens>ErrException: <METHOD DOES NOT EXIST>zqueueRequestSync+39 ^Ens.Host.1 *%OpenId,%Library.DynamicObject -- logged as '-'
number - @''

Thanks Beforehand for all the help! 

Product version: IRIS 2023.2
Discussion (6)2
Log in or sign up to continue

Here you can see an example:

ClassMethod ImportRawMatches() As %DynamicObject
    Try {
        Do ##class(%REST.Impl).%SetContentType("application/json")
        If '##class(%REST.Impl).%CheckAccepts("application/json") Do ##class(%REST.Impl).%ReportRESTError(..#HTTP406NOTACCEPTABLE,$$$ERROR($$$RESTBadAccepts)) Quit
        set newRequest = ##class(QUINIELA.Message.OperationRequest).%New()
        set newRequest.Operation = "Import"
        set status = ##class(Ens.Director).CreateBusinessService("QUINIELA.BS.FromWSBS", .instance)
       	set response = ##class(QUINIELA.Message.ImportResponse).%New()
        set response.Status = "In Process"
        set response.Operation = "Import"
        set status = instance.SendRequestAsync("QUINIELA.BP.ImportBPL", newRequest, .response)
        if $ISOBJECT(response) {	        
            Do ##class(%REST.Impl).%SetStatusCode("200")
            return response.%JSONExport()
    } Catch (ex) {
        Do ##class(%REST.Impl).%SetStatusCode("400")
        return ex.DisplayString()

The key is this row:

return response.%JSONExport()