Example using %Net.HttpRequest

170 views
Skip to first unread message

dawn

unread,
Nov 12, 2009, 9:42:33 AM11/12/09
to intersystems.public.cache
Does anyone have a snippet showing an example (outside of the class
documentation example) of using %Net.HttpRequest and working with an
XML response (to some "rest" web service url)? Thanks in advance. --
dawn

Sungung

unread,
Nov 12, 2009, 9:36:26 PM11/12/09
to intersystems.public.cache
Hi Dawn,

You can try the below snippet. If you know reponse xml schema, you can
create cache class based on that xml, and then convert reponse xml
stream to cache object.

set httpreq = ##class(%Net.HttpRequest).%New()
set httpreq.Server="twitter.com"
do httpreq.Get("/statuses/public_timeline.xml")
do httpreq.HttpResponse.Data.OutputToDevice()
quit

Hope this helps you, Sungung

Tony

unread,
Nov 13, 2009, 5:15:12 AM11/13/09
to intersystems.public.cache
Hello Dawn - the following link may be of some interest. /Tony

<http://www.cachewiki.org/index.php/Sms_alerts_for_incoming_e-
mails#SmsMessage_class>

Renato Fagalde

unread,
Nov 12, 2009, 10:09:15 AM11/12/09
to intersystems...@googlegroups.com
Hi Dawn.

   You wish open a URL(WS-server) using HttpRequest and parsing the response content step by step!? 

  

[]'s

      


2009/11/12 dawn <dawnwo...@gmail.com>



--
11 7737 - 7571
100*20132

Renato Fagalde

unread,
Nov 12, 2009, 1:33:53 PM11/12/09
to intersystems...@googlegroups.com
Hi Dawn.

    I don't understand your question! 

    Are you working in the client side or server side? I think that REST is then "response" to be developed following the "request(CONTENT-TYPE)" sent.

    Attached you´ll find one sample of how to read the tags in the body of XML. Using this sample you can learn how to do it in others cases.

   As far as I kwon "REST" is a new concept, and If you´d like to develop one JSON I belive that you´ll not find any native class.    

  
[]'s      




Class forum.bean.Car Extends (%RegisteredObject, %XML.Adaptor) [ ProcedureBlock ]
{
   property name as %String;
   property sport as %Boolean;
}


Class forum.web.WSHand Extends %CSP.Page [ ProcedureBlock ]
{

Parameter CONTENTTYPE = "text/xml";

ClassMethod OnPage() As %Status
{
   write "<?xml version=""1.0"" ?>"_
            "<root><factory><Cars><Car><name>Beetle</name><sport>0</sport></Car><Car><name>Mustang</name><sport>1</sport></Car><Car><name>Veyron</name><sport>1</sport></Car></Cars></factory></root>"
   quit $$$OK
}

}

result:

  <?xml version="1.0" ?>
- <root>
- <factory>
- <Cars>
- <Car>
  <name>Beetle</name>
  <sport>0</sport>
  </Car>
- <Car>
  <name>Mustang</name>
  <sport>1</sport>
  </Car>
- <Car>
  <name>Veyron</name>
  <sport>1</sport>
  </Car>
  </Cars>
  </factory>
  </root>
 

Class forum.core.Tools Extends %RegisteredObject [ ProcedureBlock ]
{

classmethod getURL() {


   write #

   set request = ##class(%Net.HttpRequest).%New(),
         request.Server = "127.0.0.1",
         request.Port = 1972

   do request.Get("/csp/user/forum.web.WSHand.cls")

   set response = request.HttpResponse


   set body = request.HttpResponse.Data

   set nodeHeader = "",
         nodeHeader = response.GetNextHeader(nodeHeader)

    while (nodeHeader'="") {

      write nodeHeader_":",?15,response.Headers(nodeHeader),!

      set nodeHeader = response.GetNextHeader(nodeHeader)
   }

   for iC=1:1:80 write "#" write:( (iC=1) ! (iC=80)) !!

   set reader = ##class(%XML.Reader).%New()
   do reader.OpenStream(body)

   do reader.Correlate("Car","forum.bean.Car")

   write !?10,"Name",?30,"Sport",!!

   while (reader.Next(.carro,.sc)) {

       write ?10,carro.name,?30,carro.sport,!

   }

}

}


USER>d ##class(forum.core.Tools).getURL()
 
 
CACHE-CONTROL: no-cache
CONNECTION:    Close
CONTENT-TYPE:  text/xml; charset=ISO-8859-1
DATE:          Thu, 12 Nov 2009 18:45:34 GMT
EXPIRES:       Thu, 29 Oct 1998 17:04:19 GMT
PRAGMA:        no-cache
SET-COOKIE:    CSPSESSIONID=932099751217672685; path=/csp/user/;
#
 
###############################################################################
 
 
          Name                Sport
 
          Beetle              0
          Mustang             1
          Veyron              1
 
USER>
 
2009/11/12 Renato Fagalde <fagald...@gmail.com>



--
cel:    55 11 7737 - 7571
radio: 55*100*20132

Fagalde

unread,
Nov 12, 2009, 2:11:00 PM11/12/09
to intersystems.public.cache
Reply all
Reply to author
Forward
0 new messages