Sending An HTTP Request using Intersystems Cache

4k views Asked by At

I have the following Business Process defined within a Production on an Intersystems Cache Installation

    /// Makes a call to Merlin based on the message sent to it from the pre-processor
Class sgh.Process.MerlinProcessor Extends Ens.BusinessProcess [ ClassType = persistent, ProcedureBlock ]
{

Property WorkingDirectory As %String;

Property WebServer As %String;

Property CacheServer As %String;

Property Port As %String;

Property Location As %String;

Parameter SETTINGS = "WorkingDirectory,WebServer,Location,Port,CacheServer";

Method OnRequest(pRequest As sgh.Message.MerlinTransmissionRequest, Output pResponse As Ens.Response) As %Status
{

    Set tSC=$$$OK


    Do ##class(sgh.Utils.Debug).LogDebugMsg("Packaging an HTTP request for Saved form "_pRequest.DateTimeSaved)

    Set dateTimeSaved       = pRequest.DateTimeSaved
    Set patientId           = pRequest.PatientId
    Set latestDateTimeSaved = pRequest.LatestDateTimeSaved
    Set formName            = pRequest.FormName
    Set formId              = pRequest.FormId
    Set episodeNumber       = pRequest.EpisodeNumber
    Set sentElectronically  = pRequest.SentElectronically
    Set styleSheet          = pRequest.PrintName

    Do ##class(sgh.Utils.Debug).LogDebugMsg("Creating HTTP Request Class")

    set HTTPReq = ##class(%Net.HttpRequest).%New()

    Set HTTPReq.Server      = ..WebServer
    Set HTTPReq.Port        = ..Port

    do HTTPReq.InsertParam("DateTimeSaved",dateTimeSaved)
    do HTTPReq.InsertParam("HospitalNumber",patientId)
    do HTTPReq.InsertParam("Episode",episodeNumber)
    do HTTPReq.InsertParam("Stylesheet",styleSheet)
    do HTTPReq.InsertParam("Server",..CacheServer)

    Set Status = HTTPReq.Post(..Location,0) Quit:$$$ISERR(tSC)

    Do ##class(sgh.Utils.Debug).LogDebugMsg("Sent the following request: "_Status)

    Quit tSC
}

}

The thing is when I check the debug value (which is defined as a global) all I get is the number '1' - I have no idea therefore if the request has succeeded or even what is wrong (if it has not)

What do I need to do to find out

A) What is the actual web call being made?

B) What the response is?

3

There are 3 answers

0
Derek On

I believe the answer you want to A) is in the Server and Location properties of your %Net.HttpRequest object (e.g., HTTPReq.Server and HTTPReq.Location).

For B), the response information should be in the %Net.HttpResponse object stored in the HttpResponse property (e.g. HTTPReq.HttpResponse) after your call is completed.

I hope this helps!

-Derek

(edited for formatting)

0
mccrackend On

There is a really slick way to get the answer the two questions you've asked, regardless of where you're using the code. Check the documentation out on the %Net.HttpRequest object here: http://docs.intersystems.com/ens20102/csp/docbook/DocBook.UI.Page.cls?KEY=GNET_http and the class reference here: http://docs.intersystems.com/ens20102/csp/documatic/%25CSP.Documatic.cls?APP=1&LIBRARY=ENSLIB&CLASSNAME=%25Net.HttpRequest

The class reference for the Post method has a parameter called test, that will do what you're looking for. Here's the excerpt:

method Post(location As %String = "", test As %Integer = 0, reset As %Boolean = 1) as %Status

Issue the Http 'post' request, this is used to send data to the web server such as the results of a form, or upload a file. If this completes correctly the response to this request will be in the HttpResponse. The location is the url to request, e.g. '/test.html'. This can contain parameters which are assumed to be already URL escaped, e.g. '/test.html?PARAM=%25VALUE' sets PARAM to %VALUE. If test is 1 then instead of connecting to a remote machine it will just output what it would have send to the web server to the current device, if test is 2 then it will output the response to the current device after the Post. This can be used to check that it will send what you are expecting. This calls Reset automatically after reading the response, except in test=1 mode or if reset=0.

I recommend moving this code to a test routine to view the output properly in terminal. It would look something like this:

// To view the REQUEST you are sending
Set sc = request.Post("/someserver/servlet/webmethod",1)

// To view the RESPONSE you are receiving
Set sc = request.Post("/someserver/servlet/webmethod",2)

// You could also do something like this to parse your RESPONSE stream
Write request.HttpResponse.Data.Read()
1
Clayton On

From that code sample it looks like you're using Ensemble, not straight-up Cache.

In that case you should be doing this HTTP call in a Business Operation that uses the HTTP Outbound Adapter, not in your Business Process.

See this link for more info on HTTP Adapters: http://docs.intersystems.com/ens20102/csp/docbook/DocBook.UI.Page.cls?KEY=EHTP

You should also look into how to use the Ensemble Message Browser. That should help with your logging needs.