Eduard Lebedyuk · Mar 17, 2016

How to get full URL from %Net.Request object after request

I have a %Net.Request object and I want to get a full URL of the request sent (preferably after all redirects, but even full initial one would be good). For example, I have the following method:

ClassMethod GetBingRequest() As %Net.HttpRequest
    #dim request As %Net.HttpRequest
    set request = ##class(%Net.HttpRequest).%New()
    set request.Server = ""
    set request.Https = $$$YES
    set request.SSLConfiguration = "Bing"
    set request.Username = ..GetBingAPIKey()
    set request.Password = ..GetBingAPIKey()
    set request.Location = "Data.ashx/Bing/SearchWeb/v1/Composite"
    do request.SetParam("$top", 1)
    do request.SetParam("$format", "JSON")
    quit request

And I invoke it with the method:

ClassMethod Get(Query As %String) {
    set request = ..GetBingRequest()
    do request.SetParam("Query", "'" _ Query _ "'")
    do request.Get()

How can I get the full URL of where the request went?

P.S. This is a working code for Bing Search API – Web Results Only .

0 1,321
Discussion (8)0
Log in or sign up to continue

#dim url = request.Server_":"_request.Port_"/"_request.Location

Or do you mean something else?

There is more to URL then just that. For example:

  • Http/Https
  • Request parameters
  • Redirects

I'd suggest something like this, after calling Get:

Set tParams = request.ReturnParams()
Set tQuery = $Case(tParams,"":"",:"?"_tParams)
Set tURL = $Select(request.Https:"https://",1:"http://")_request.Server_":"_request.Port_"/"

Actually - if you're going to do this after issuing the Get, you should use request.Get(,,0), then do this, then call Reset() on the request manually. Otherwise the parameters will get cleared out and the query string will always be blank.

Thanks. I thought it would be something like this, but hoped there was a system method somewhere.

Yeah, that would be nice to have. I looked a bit and didn't see one right away. (I'd actually been wondering the same thing last night, as it happens.)

Why do you need this?

For example, for debugging purposes you might use second argument of Get method:

do request.Get(,1)

In that case "instead of connecting to a remote machine httprequest will just output what it would have send to the web server to the current device".

It's for translating response status into Caché status. I have the following code:

ClassMethod GetResponseStatus(Request As %Net.HttpRequest) As %Status
    Set Status = Request.HttpResponse.StatusCode
    Quit:(Status = 200) $$$OK
    Set Body = Request.HttpResponse.Data.Read($$$MaxCacheInt)
    Quit $$$ERROR($$$GeneralError,"Status code: " _ Status _ " ReasonPhrase: " _ Request.HttpResponse.ReasonPhrase _ " StatusLine: " _ Request.HttpResponse.StatusLine _ " Body: " _ Body)

And I want to add URL reporting to it.