Question
· Dec 3, 2023

Getting posted JSON in a service production

Hi Guys,

I have a client posting me JSNON file using a Get action and I'm using the below code to retrieve the data:

Class SX3.Production.HTTP.GetPSRequest Extends Ens.BusinessService [ ClassType = "", ProcedureBlock ]
{ Parameter ADAPTER = "EnsLib.HTTP.InboundAdapter"; Method OnProcessInput(pInput As %CharacterStream, Output pOutput As %CharacterStream) As %Status
{
    Set pOutput=##class(%GlobalCharacterStream).%New()
    Set tSC=##class(Ens.Util.JSON).ObjectToJSONStream(pInput,.tJsonPayload,"aceloqs")
    set jsonString = tJsonPayload.Read($$$MaxStringLength)
      ^data=jsonString
 

issue is I'm getting empty string in my ^data global 

this is the post from my clients when he gets 200 response

GET /SX HTTP/1.0\r\nHost: live.hub.com\r\n{“facility_id”:”123456789”, ”print_job_request”}\r\n\r\n

and if posting it using Post action 

POST /SX HTTP/1.0\r\nHost: live.hub.com\r\n{“facility_id”:”123456789”, ”print_job_request”}\r\n\r\n

he gets a 500 response  

Thanks

Product version: Ensemble 2018.1
Discussion (6)2
Log in or sign up to continue

I see a few issues in your sample.

First, it's no clear what' your goal, what do you need to do with the received json?

Note that what is posted:
{"facility_id":"123456789", "print_job_request"}
is not a valid json, so whatever you do, it will fail.

The ObjectToJSONStream() method "convert" an object to a JSON stream.
In your case you have a json stream and.....I'm not sure what you need to do.

Assuming the json is valid (and now isn't) you can load the json stream into a %Library.DynamicObject using:

Set dynOBJ={}.%FromJSON(pInput)

Now you can access properties like:

Set ^data=dynOBJ."facility_id" ; quotes are needed because of _

To get the json string back:

Set ^data(1)=dynOBJ.%ToJson()

HTH,

Enrico

Thanks Enrico, I was so focused on my end of code and I didn't notice the wrong JSON that my client is sending, but BTW my client is running Ensemble 2014 and Set dynOBJ={}.%FromJSON(pInput) is raising an error maybe this is one of the latest versions Set dynOBJ = ##class(%Object).%FromJSON(pInput) doesn't raise errors so will try that ?
but in this case does my client need to send with Get or Post action?


    
 
Thanks 

Are you calling %FromJSON passing a valid JSON or an invalid JSON?
If you have an invalid JSON, whatever you do, it will fail.

If you pass an invalid JSON, then:

USER>w $zv
Cache for Windows (x86-64) 2018.1.2 (Build 309U) Mon Mar 4 2019 15:05:44 EST
USER>Set stream=##class(%Stream.TmpCharacter).%New() 
USER>Do stream.Write("{""facility_id"":""123456789"", ""print_job_request""}")
USER>Set dynOBJ={}.%FromJSON(stream)
<THROW>%FromJSON+37^%Library.DynamicAbstractObject.1 *%Exception.General Parsing error 3 Line 1 Offset 48

If you pass a valid JSON:

USER>w $zv
Cache for Windows (x86-64) 2018.1.2 (Build 309U) Mon Mar 4 2019 15:05:44 EST
USER>Set stream=##class(%Stream.TmpCharacter).%New() 
USER>Do stream.Write("{""facility_id"":""123456789"", ""print_job_request"":""123""}")
USER>Set dynOBJ={}.%FromJSON(stream)
USER>write dynOBJ.%ToJSON()
{"facility_id":"123456789","print_job_request":"123"}
USER>

POST or GET depends on what's your goal, what your service is supposed to do.

Enrico

Your question reports:

Product version: Ensemble 2018.1

My answer is for that version.

Sorry, I don't have any 2014 version available and I really don't remember if/what something is available in that old version regarding JSON.

For sure I can tell you that ##class(%Object).%FromJSON(pInput) makes no sense.
I pretty sure that %DynamicObject was not available back then (so, no {} object).

It's about time to upgrade or in fact, migrate to IRIS. 😊

Enrico