Question
· Feb 3, 2022

Writing big JSON xml object to httpRequest.EntityBody in POST method not working

Hi Community,

I have json equivalent persistent object in pjsonObj variable. Final JSON example below.

{
     "Document": {
                    "FileExtension": "pdf",
                    "FileContent": "JVBERi0xLjUNCiW1tbW1DQo...(very large bsae64 string)"
                }
}

Here FileContent field in json will contain converted base64 string of very large pdf(10pages). I need to submit this json as a payload to REST endpoint.

below code is working fine. 

set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.newobj,pjsonObj,,,1,"aelqtw")
$$$LOGINFO("json"_newobj.Read())

Set httpRequest= ##class(%Net.HttpRequest).%New()
Set httpRequest.Server = ..Adapter.HTTPServer
Set httpRequest.Location = ..Adapter.URL
Set httpRequest.SSLConfiguration = ..Adapter.SSLConfig
Set httpRequest.SSLCheckServerIdentity = 0
Set httpRequest.Https = $$$YES
Set httpRequest.ContentType = "application/json"
Set httpRequest.Authorization = "Bearer "_pToken
set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(httpRequest.EntityBody,pjsonObj,,,1,"aelqtw")
Set sc = httpRequest.Post("", 2)

Here I have 2 questions.

1) Is my custom code is correct for rest post call for my big json stream requirement? but I have tested working fine my few tests.

2) What is the importance of first 2 lines from this code? if I commented that 2 lines , whole stream is not converting as payload as a result full json is not attached to request. but REST call is successful. getting error response from target system.

As per my knowledge , commented lines should not effect the stream conversion of httpRequest.EntityBody.

//set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(.newobj,pjsonObj,,,1,"aelqtw")
//$$$LOGINFO("json"_newobj.Read())

Set httpRequest= ##class(%Net.HttpRequest).%New()
Set httpRequest.Server = ..Adapter.HTTPServer
Set httpRequest.Location = ..Adapter.URL
Set httpRequest.SSLConfiguration = ..Adapter.SSLConfig
Set httpRequest.SSLCheckServerIdentity = 0
Set httpRequest.Https = $$$YES
Set httpRequest.ContentType = "application/json"
Set httpRequest.Authorization = "Bearer "_pToken
set tSC=##class(%ZEN.Auxiliary.jsonProvider).%WriteJSONStreamFromObject(httpRequest.EntityBody,pjsonObj,,,1,"aelqtw")
Set sc = httpRequest.Post("", 2)

Please suggest any best way to code for the same requirement? We need to go live on next week,Could you tell me what changes required in this?

 

Thanks in Advance,

Prashanth

Product version: Caché 2017.1
Discussion (1)1
Log in or sign up to continue