Written by

Senior Cloud Architect at InterSystems
Question 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 = "api.datamarket.azure.com"
    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 .

Comments

Jochen Roese · Mar 17, 2016

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

Or do you mean something else?

0
Eduard Lebedyuk  Mar 17, 2016 to Jochen Roese

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

  • Http/Https
  • Request parameters
  • Redirects
0
Timothy Leavitt · Mar 17, 2016

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_"/"
    _request.Location_tQuery
0
Timothy Leavitt  Mar 17, 2016 to Timothy Leavitt

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.

0
Eduard Lebedyuk  Mar 17, 2016 to Timothy Leavitt

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

0
Timothy Leavitt  Mar 17, 2016 to Eduard Lebedyuk

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.)

0
Alexander Koblov · Mar 17, 2016

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".

0
Eduard Lebedyuk  Mar 18, 2016 to Alexander Koblov

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.

0