HTTP Request - WS REST

Hello Guys,

Can someone help me?

I'm triggering a Rest Services, my JSON is a String variable(Request.JSON), when I pass it on EntityBody.Write, WS returns me the 403 error.

When I pass Obj = {} it responds by saying that no Data has been sent.

So, my problem is to set the "Obj" with the String variable, I should convert it, correct? But how do I do this? My JSON is all in this variable.

 

MyCode:

        Set Dados = ##class(%Net.HttpRequest).%New(),
            Dados.Authorization = pBearer _ Request.Token,
            Dados.ContentType = "application/json"
 
        $$$TRACE(Dados.Authorization)
        Set credential = ##class(Ens.Config.Credentials).%OpenId("demonstracao")
        //Set Obj = Request.JSON.%ToStream
        Set Obj = Request.JSON
        $$$TRACE("Aqui3")
        Do Dados.EntityBody.Write(Obj.%ToJSON())

 

 

Thanks

 

  • 0
  • 0
  • 127
  • 0
  • 2

Answers

Hello, check if this works for you.
If what you are getting in Request.JSON is a String, put it this way:

ClassMethod Test() As %Status
{ 
SET Data = ##class(%Net.HttpRequest).%New() 
SET Obj = "{teste:1}"

DO Data.EntityBody.Write(Obj)
 
Quit 1
}

If you are receiving a Stream, do this:

DO Data.EntityBody.Write(Obj.Read())

Edrian,

You state that Request.JSON is a simple string.  The %ToJSON() method only exists as part of the DynamicObject and DynamicArray classes.  Actually I am surprised that this is not failing completely before you even send the request because of this.  If your variable already has well formed JSON as the value then you can just write that into the EntityBody.

 

BTW, When you deal with JSON in the future you may find an advantage in using the Dynamic object handling for JSON.  For example take the following JSON {"NAME":"RICH","STATE":"MA"}

If this is in a variable, say jsonStr, I can load this into a Dynamic Object using the following command:

set obj = {}.%FromJSON(jsonStr)

Now you can use object references to work with the JSON

Write obj.NAME   -> displays RICH

I can add new properties dynamically

set obj.ZIPCODE = "99999"

Finally convert this back to a json string with:

write obj.%ToJSON()

which would display  {"NAME":"RICH","STATE":"MA","ZIPCODE":"99999"}

See the documentation at https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GJSON_preface