Response not returning to BPL
I getting the response back to my method fine,
.png)
but it is not returning to my BPL.
.png)
What am I missing to get the response back to the BPL?
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)
}
Comments
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
|
ERROR <Ens>ErrGeneral: Retrying Message body 6@AH.AHLIB.Custom.Symedical.Message.PostRelationRequest / 1200944442 because response 8@AH.AHLIB.Custom.Symedical.Message.PostRelationResponse Status 'ERROR #9406: Unexpected format for value of field, Response, using class base mapping' matched ReplyCodeAction 1 : 'E=RF' resulting in Action code RF |
Response is an array
so my respose class is defined as,
class AH.AHLIB.Custom.Symedical.Message.PostRelationResponse.response is defined as,
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"}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":[
.
.
.
.
]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 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.....
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