Question
· Nov 28, 2023

Response not returning to BPL

I getting the response back to my method fine,

but it is not returning to my BPL.

What am I missing to get the response back to the BPL?

// Execute REST Call
set tHttpResponse=##class(%Net.HttpResponse).%New()
set tSC = ..Adapter.SendFormDataArray(.tHttpResponse,"POST",tHttpRequest,"","",tURL)
set tStatusCode = tHttpResponse.StatusCode
$$$TRACE("Status Code: ["_tStatusCode_"]")
do ..logJSON(tHttpResponse.Data.Read(),"Post Relation Response")

// On HTTP 200 - OK
if tStatusCode = 200 {
     do tHttpResponse.Data.Rewind()
     do pResponse.%JSONImport(tHttpResponse.Data.Read())
     quit $$$OK
else {
     quit $$$ERROR("5001","["_tStatusCode_"] "_"Error occurred. " _ tHttpResponse.StatusLine)
}

Product version: IRIS 2022.2
Discussion (8)2
Log in or sign up to continue

Response is an array

so my respose class is defined as,

Property Response As list Of AH.AHLIB.Custom.Symedical.Message.PostRelationResponse.response;

class AH.AHLIB.Custom.Symedical.Message.PostRelationResponse.response is defined as,

Parameter %JSONIGNOREINVALIDFIELD = 1;

Property RequestedTerm As AH.AHLIB.Custom.Symedical.Message.PostRelationRequest.termsRequested;

Property Messages As %Library.DynamicArray;

Property RelatedItems As %Library.DynamicArray;

Property SharedRelations As %Library.DynamicArray;
 

Last three properties are arrays, but I don't need their contents. Do I define them right?

Hello @Michael Wood

It won't allow to import the array values directly from the response/payload If you're using %JSONImport  in %JSON.Adaptor due to the datatype validation while import. So, You may define the list of properties in separate class definition and use that Property Messages As list of class if the array has object values and It allows to import. Or if it's literal string inside an array then it allows without any issues. 

Sample: Literal string values inside array

Class Samples.NewClass2 Extends (%Persistent, %Populate, %JSON.Adaptor)
{

Property email As list Of %String;

Parameter %JSONIGNOREINVALIDFILED = 1;

Property FirstName As %String;

Property LastName As %String;

ClassMethod test()
{
    set json={"FirstName":"test","LastName":"lastname","email":["test@gmail.com","test2@gmail.com"]}
    set obj= ..%New()
    set st= obj.%JSONImport(json)
    if $$$ISERR(st) write $SYSTEM.OBJ.DisplayError(st)
    write obj.%JSONExport()
}

}

output

LEARNING>d ##Class(Samples.NewClass2).test()
{"email":["test@gmail.com","test2@gmail.com"],"FirstName":"test","LastName":"lastname"}

Hi Michael,

in order for %JSONImport() method to work properly the class of "pResponse" (inheriting from %JSON.Adaptor) MUST match the structure of the JSON being imported.

From the error it seems this is not the case for your class and JSON.

Another option is to load/import the JSON stream into a dynamic object {} (%Library.DynamicObject) and then "manually" set your pResponse properties from the dynamic object properties.

Enrico

I tried this,

I set pResponse As %String for the method output

set pResponse=""
set formatter = ##class(%JSON.Formatter).%New()
set tJSONFormattedString=""
set tSC = formatter.FormatToString(tHttpResponse.Data.Read(),.tJSONFormattedString)
set pResponse = tJSONFormattedString
quit pResponse

It produces this error

ERROR <Ens>ErrGeneral: Quitting with error on Message body 6@AH.AHLIB.Custom.Symedical.Message.PostRelationRequest / 1201174076 because Status 'ERROR #5034: Invalid status code structure ("["_$c(13,10)_" {"_$c(13,10)_" ""RequestedTerm"":{"_$c(13,10)_" ""CatalogIdentifier"":""S_ASC_CARE_BRANCH"","_$c(13,10)_" ""TermSourceCode"":""ASC-BRC-000179"","_$c(13,10)_" ""IncludeDomainCharacteristics"":true"_$c(13,10)_" },"_$c(13,10)_" ""Messages"":["_$c(13,10)_" ],"_$c(13,10)_" ""RelatedItems"":["_$c(13,10)_" {"_$c(13,10)_" ""RelationshipUID"":""1146f07d-9c93-4c4e-82ba-0989fa6a62d9"","_$c(13,10)_" ""RelationshipMnemonic"":""HAS_SITECORE_ID"","_$c(13,10)_" ""CatalogU.....