Question
Ali Chaib · Apr 1, 2020

Post standard HTTP requests to the server with special parameters on the URL, the server returns data records instead of HTML pages

Hi,

 Kindly find the below example :

If I open google chrome and I add the below link (just an example)  , it will download a file to my local PC.

https://www.abc.com/cgi-bin/ocs/pswd=123&rpt=1

pswd correspond to the password 

rpt represent the report

 

How can I do the above using code in cache studio ? 

What type of services should I use? what's the parameters ? functions?  and how can I test it?

How to post a standard HTTP requests to the server with special parameters on the URL while the server returns data records instead of HTML pages ?

Thank you in advance.

0
0 271
Discussion (4)2
Log in or sign up to continue

Sorry, but it's not clear to me what you want to achieve.

If you want to provide data if somebody connected to your server,  then use the DC.Upload class (see below).

If somebody has data for you and you want to programatically download this data, use the DC.Download class (see below).

The examples below does not handle the case, where the (Cache/IRIS)Server needs an user authentication (my IRIS  System > Security Management > Web Applications is set to "Unauthenticated")

Server side (Upload)

/// Upload (i.e. provide) data to a remote party
Class DC.Upload Extends %CSP.Page
{
ClassMethod OnPage() As %Status
{
   if %request.Data("myData")]"" {
      do %request.Data("myData").OutputToDevice()
      do %request.Data("myData").%Close() }
   else {
      write "<html><head></head><body>",!
      write "Please provide a correct Password and ReportId<br>",!
      write "</body></html>",!
   }
   quit $$$OK
}

ClassMethod OnPreHTTP() As %Boolean [ ServerOnly = 1 ]
{
   if ..chkPsw(), ..chkRpt(.file) {
      set data=##class(%FileBinaryStream).%New()
      set size=##class(%File).GetFileSize(file)
      set name=##class(%File).GetFilename(file)
      do data.LinkToFile(file)
      set %response.ContentType="application/pdf"
      do %response.SetHeader("Content-Disposition","attachment;filename="_name)
      do %response.SetHeader("Content-Length",size)
      set time=$h-1
      set %response.Expires=$zd(time,11)_", "_$zd(time,2)_" 00:00:00 GMT"
      set %request.Data("myData")=data
 
      } else {
         set %request.Data("myData")=""
      }
  
      quit $$$OK
}

ClassMethod chkPsw()
{
   set psw=$g(%request.Data("pswd",1))
   if psw]"" quit 1
   quit 0
}

ClassMethod chkRpt(name)
{
   set rpt=$g(%request.Data("rpt",1))
   if rpt]"" {
      set name="/tmp/outfiles/74LS13.pdf"
      quit 1
   }
   quit 0
} 

}

Client side (download)

/// Download (i.e. get) data from remote server
/// 
Class DC.Download Extends %RegisteredObject
{
ClassMethod GetFile(psw, rpt, saveTo = "/tmp/inpfiles/")
{
   set http=##class(%Net.HttpRequest).%New()
   set http.Server="localhost"
   set http.Port=52773
   do http.SetParam("pswd",psw)
   do http.SetParam("rpt",rpt)

   if http.Get("/csp/user/DC.Upload.cls") {
      set file=$piece($g(http.HttpResponse.Headers("CONTENT-DISPOSITION")),"=",2)
      set del=$select($zversion(1)=2:"\", 1:"/")
      set file=saveTo_$s($e(saveTo,*)=del:"",1:del)_$s(file="":"noname.dat",1:file)
      
      open file:"nwu":0
      if $t {
         use file
         do http.HttpResponse.Data.OutputToDevice()
         close file
         quit 1

      } else {
        quit "0,Can't open "_file
      }

   } else {
       quit "0,"_http.HttpResponse.StatusLine
   }
} 
}


I really appreciate your help !

Thank you sooo much :)

It worked from first time ! 

Oh, I just see now, I forgot to add the two test lines

Enter into browser : http://localhost:52773/csp/user/DC.Upload.cls?pswd=123&rpt=456

Enter into Terminal:

IDEV:USER>write ##class(DC.Download).GetFile(123,456,"/tmp/inpfiles/")  ==> 1