Written by

Deloitte
Question Michael Wood · 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

Comments

Enrico Parisi · Nov 28, 2023

Maybe the %JSONImport method is returning an error?

To find it out change this two lines:
do pResponse.%JSONImport(tHttpResponse.Data.Read())
quit $$$OK

With:
quit pResponse.%JSONImport(tHttpResponse.Data.Read())

Then check the trace/event log.

Enrico

0
Michael Wood · Nov 28, 2023

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?

0
Ashok Kumar T  Nov 29, 2023 to Michael Wood

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"}
0
Michael Wood  Nov 29, 2023 to Ashok Kumar T

does the same apply for if it is an array? The first character is a [

[
   {
      "RequestedTerm":{
      "CatalogIdentifier":"S_ASC_CARE_BRANCH",
      "TermSourceCode":"ASC-BRC-000179",
      "IncludeDomainCharacteristics":true
   },
   "Messages":[
.
.
.
.
]
0
Enrico Parisi · Nov 28, 2023

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

0
Michael Wood · Nov 29, 2023

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.....

0
Enrico Parisi  Nov 29, 2023 to Michael Wood

Where does that code comes from?

It seems that your method expect a %Status as returned value, try with:

Quit tSC

Like you did in your first example.

Enrico

0