Get a value from a REST serviceTag(s): WinApi/Registry
From this HowTo, we saw how to get a value from a JSON string. Habitually, we get a JSON string from a Web service exposed as REST service. It maybe difficult for Powerbuilder to call this kind of service because there is no way to specify HTTP header with regular Powerscript.
Fortunately, we can use two Windows COM objects, Msxml2.DOMDocument and MSXML2.ServerXMLHTTP, to do that.
For demonstration, we call a REST service that return the current date and time. This service is hosted at http://www.jsontest.com/. See the important note below.
OleObject lole_Send
OleObject lole_SrvHTTP
OleObject lole_ScriptControl
String ls_status
String ls_response
String ls_value
lole_Send = CREATE OleObject
lole_SrvHTTP = CREATE OleObject
lole_Send.connectToNewObject("Msxml2.DOMDocument.6.0")
lole_SrvHTTP.connectToNewObject("MSXML2.ServerXMLHTTP.6.0")
lole_SrvHTTP.Open("GET", "http://date.jsontest.com", FALSE)
lole_SrvHTTP.SetRequestHeader( "Content-Type", "application/json")
lole_SrvHTTP.Send(lole_Send)
ls_status = string(lole_SrvHTTP.Status)
ls_response = string(lole_SrvHTTP.ResponseText)
MessageBox("REST HTTP response", ls_status)  // 200 is OK!
MessageBox("JSON response", ls_response)
lole_ScriptControl = CREATE OleObject
lole_ScriptControl.ConnectToNewObject( "MSScriptControl.ScriptControl" )
lole_ScriptControl.Language = "JScript"
lole_ScriptControl.AddCode("function getValue(s,key) {eval(~"jsonobj=~" + s); return eval(~"jsonobj.~" + key) ; }")
TRY
  // remove (replace with "") all the carriage return to clean up the JSON string
  int position, i
  FOR i = 1 to len(ls_response)
        position = pos(ls_response, "~n")
        IF position > 0 THEN
            ls_response= Replace(ls_response, position, 1, "")
        END IF
  NEXT
  MessageBox("JSON response cleanup", ls_response)
  // typical response
  //   { "time": "04:21:52 PM",  "milliseconds_since_epoch": 1436113312190, "date": "07-05-2015" }
   ls_value =  lole_ScriptControl.Eval ("getValue(' " + ls_response + " ',  'date');")
   MessageBox("date value", ls_value)
CATCH ( Throwable e )
   MessageBox("Err", e.GetMessage())
END TRY
IMPORTANT NOTE
You have to make sure that the JSON string is valid and from a trusted source because this technique (using eval() and not a real JSON parser) is not safe and can be used to inject and execute malicious code.
See also this HowTo : Get a value from a JSON string.
  mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com
