Sending data in JSON format using Cache HTTPRequest

472 views
Skip to first unread message

Alex Grishkan

unread,
Sep 24, 2015, 4:39:05 AM9/24/15
to Caché, Ensemble, DeepSee
Hi,

I am trying to send data in JSON format to a web site:

{
"account": 484,
"vendor": 6,
"tests":
[
{
"test": 1269,
"name": "CBC"
}
]
}

The following code works fine with single value elements but fails sending the "tests" object. I'm doing something wrong but cannot figure out what,

s rObj=##class(%Net.HttpRequest).%New()
s rObj.Server="someserver.com"
s rObj.Https=1
s rObj.SSLConfiguration="SomeSSL"
d rObj.InsertFormData("account", 484)
d rObj.InsertFormData("vendor", 66)
d rObj.InsertFormData("tests", "[ { ""test"":""1269"", ""name"":""CBC"" } ]")
s sc=rObj.Post("/api/order")

Any help would be greatly appreciated.
Thank you. 

Dmitry Maslennikov

unread,
Sep 24, 2015, 5:55:29 AM9/24/15
to Caché, Ensemble, DeepSee
With a version less then 2015.3, you should define some object with %ArrayOfDataTypes and arrays with %ListOfDataTypes
and with class like here, convert it to json string

set rObj=##class(%Net.HttpRequest).%New()
set rObj.Server="someserver.com"
set rObj.Https=1
set rObj.SSLConfiguration="SomeSSL"

set obj=##class(%ArrayOfDataTypes).%New()
do obj.SetAt(484,"account")
do obj.SetAt(6,"vendor")
set tests=##class(%ListOfDataTypes).%New()
set test=##class(%ArrayOfDataTypes).%New()
do test.SetAt(1269,"test")
do test.SetAt("CBC","name")
do tests.Insert(test)
do obj.SetAt(tests,"tests")

set json=##class(Utils.JSON).Encode(obj)
set rObj.ContentType="application/json"
do rObj.EntityBody.Write(json)
set sc=rObj.Post("/api/order",1)

so, the request should be 
POST /api/order HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; Cache;)
Accept-Encoding: gzip
Content-Length: 63
Content-Type: application/json
 
{"account":484,"tests":[{"name":"CBC","test":1269}],"vendor":6}

But with 2015.3 it will be much easier, with the same result
set rObj=##class(%Net.HttpRequest).%New()
set rObj.Server="someserver.com"
set rObj.Https=1
set rObj.SSLConfiguration="SomeSSL"

set obj={

  "account"484,
  "vendor"6,
  "tests"[
    {
      "test"1269,
      "name""CBC"
    }
  ]
}

do rObj.EntityBody.Write(obj.$toJSON())
set sc=rObj.Post("/api/order",1)



--
--
Caché, Ensemble, DeepSee

---
You received this message because you are subscribed to the Google Groups "Caché, Ensemble, DeepSee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to intersystems-publi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Dmitry Maslennikov
Lead Programmer at LETOGRAF
Maintainer of NBStudio
Skype: DAiMor

Alex Grishkan

unread,
Sep 24, 2015, 4:49:38 PM9/24/15
to Caché, Ensemble, DeepSee
Thank you
Reply all
Reply to author
Forward
0 new messages