Question
· Oct 12, 2017

How to Make a Http Request in Caché ObjectScript

Set httprequest=##class(%Net.HttpRequest).%New()
	Set httprequest.Server="www.intersystems.com"
	Do httprequest.Get("/")
	Do httprequest.HttpResponse.OutputToDevice()
	Do httprequest.Get("/cache/")
	Do httprequest.HttpResponse.OutputToDevice()

The above is the code which i found in this link http://docs.intersystems.com/latest/csp/documatic/%25CSP.Documatic.cls?P... 
How do the above code works and can i be able to run this?

Thanks in advance.

Discussion (5)0
Log in or sign up to continue

You can run this from Caché Terminal, or put it in a routine or class and run it:

USER>Set httprequest=##class(%Net.HttpRequest).%New()
 
USER>Set httprequest.Server="www.intersystems.com"
 
USER>Do httprequest.Get("/")
 
USER>Do httprequest.HttpResponse.OutputToDevice()
HTTP/1.1 301 Moved Permanently
CONNECTION: keep-alive
CONTENT-LENGTH: 178
CONTENT-TYPE: text/html
DATE: Thu, 12 Oct 2017 14:21:23 GMT
LOCATION: https://www.intersystems.com/
SERVER: nginx
X-TYPE: default
 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

The %Net.HttpRequest class provides an HTTP client for interacting with HTTP servers.

Underneath it essentially opens up a TCP port to the given IP address (DNS lookup), sends the required HTTP headers, request body / MIME parts, and waits for the response which is unpacked into a response object.

I'm not sure if you are asking if its safe to use or how to use it, so here is a reply to both.

The class is very robust and adheres to the HTTP/1.1 specification.

You will find that this class underpins many other classes in Caché / Ensemble and should be considered battle tested code that you can fully rely on.

To use it, the best way to start out would be to play around with the code in caché terminal. You can cut and paste the snippet you provided directly into the terminal (without the tabs), and this is what you will see...
 

USER>Set httprequest=##class(%Net.HttpRequest).%New()
 
USER>Set httprequest.Server="www.intersystems.com"
 
USER>Do httprequest.Get("/")
 
USER>Do httprequest.HttpResponse.OutputToDevice()
HTTP/1.1 301 Moved Permanently
CONNECTION: keep-alive
CONTENT-LENGTH: 178
CONTENT-TYPE: text/html
DATE: Thu, 12 Oct 2017 14:14:02 GMT
LOCATION: https://www.intersystems.com/
SERVER: nginx
X-TYPE: default
 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

As you can see, it returns a 301 status code because the intersystems web site is only running over HTTP on port 443, there are settings on the request object that support HTTPS.

Better to play around with a non HTTP site to begin with, here is an example with my website...

(a small trick, the command zw will dump the contents of an object to screen, but not if that object is a property of another object, so assign it to a variable and zw that variable, here you can see the status code and other response properties that you can use)
 

USER>set httprequest=##class(%Net.HttpRequest).%New()
 
USER>set httprequest.Server="www.memcog.com"
 
USER>set sc=httprequest.Get("/")
 
USER>set response = httprequest.HttpResponse
 
USER>zw response
response=<OBJECT REFERENCE>[4@%Net.HttpResponse]
+----------------- general information ---------------
|      oref value: 4
|      class name: %Net.HttpResponse
| reference count: 3
+----------------- attribute values ------------------
|    ContentBoundary = ""
|        ContentInfo = ""
|      ContentLength = 4660
|        ContentType = "text/html"
|               Data = "5@%Stream.FileCharacterGzip"
|Headers("ACCEPT-RANGES") = "bytes"
|Headers("CONTENT-ENCODING") = "gzip"
|Headers("CONTENT-LENGTH") = 4660
|Headers("CONTENT-TYPE") = "text/html"
|    Headers("DATE") = "Thu, 12 Oct 2017 14:29:58 GMT"
|    Headers("ETAG") = """66a36a6a-3f38-53cedab4fb6b1"""
|Headers("LAST-MODIFIED") = "Tue, 20 Sep 2016 10:12:42 GMT"
|  Headers("SERVER") = "Apache"
|    Headers("VARY") = "Accept-Encoding,User-Agent"
|        HttpVersion = "HTTP/1.1"
|       ReasonPhrase = "OK"
|         StatusCode = 200
|         StatusLine = "HTTP/1.1 200 OK"
+-----------------------------------------------------

USER>write response.Data.Read(1500)​

<!--
                                                         DEVELOPED BY...
 

__/\\\\____________/\\\\__/\\\\\\\\\\\\\\\__/\\\\____________/\\\\________/\\\\\\\\\_______/\\\\\__________/\\\\\\\\\\\\_
 _\/\\\\\\________/\\\\\\_\/\\\///////////__\/\\\\\\________/\\\\\\_____/\\\////////______/\\\///\\\______/\\\//////////__
  _\/\\\\///\\\/\\\/_\/\\\_\/\\\\\\\\\\\_____\/\\\\///\\\/\\\/_\/\\\__/\\\______________/\\\______\//\\\_\/\\\____/\\\\\\\_
   _\/\\\__\///\\\/___\/\\\_\/\\\///////______\/\\\__\///\\\/___\/\\\_\/\\\_____________\/\\\_______\/\\\_\/\\\___\/////\\\_
    _\/\\\____\///_____\/\\\_\/\\\_____________\/\\\____\///_____\/\\\_\//\\\____________\//\\\______/\\\__\/\\\_______\/\\\_
     _\/\\\_____________\/\\\_\/\\\_____________\/\\\_____________\/\\\__\///\\\___________\///\\\__/\\\____\/\\\_______\/\\\_
      _\/\\\_____________\/\\\_\/\\\\\\\\\\\\\\\_\/\\\_____________\/\\\____\////\\\\\\\\\____\///\\\\\/_____\//\\\\\\\\\\\\/__
       _\///______________\///__\///////////////__\///______________\///________\/////////_______\/////________\////////////____

-->

<html>
    <head>
    <title>MEMCOG: Web Development, Integration, Ensemble, Healthshare, Mirth</title>
    <link href="main.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet">
    <script src="https://use.fontawesome.com/