· Oct 4, 2016 1m read

How to Parse a URL in Caché

How to parse a URL in Caché:

Let's pretend you are working on a project where you may be retrieving a URL for some purpose and you have the need to be easily able to parse apart that URL to get the various components that make up the URL.

Here's how to do that:

Note:  This example assumes you have a variable named sURL that contains some url (i.e. and you will be creating an array of the URL component pieces, where the array will be named aComponents.

In your class/routine, add the following code:

Do ##class(%Net.URLParser).Parse(sURL,.aComponents)

This will parse apart the specified URL (see example url above) and will create the specified return array, which will contain the following nodes:

aComponents("scheme") - will contain the transport scheme (protocol) of the url (i.e. http:)

aComponents("netloc") - will contain the network address (domain) of the url (i.e.

aComponents("path") - will contain the file path within the url (i.e. /main.csp)

aComponents("query") - will contain the query string within the url (i.e. QUERY=abc)

aComponents("fragment") - will contain the fragment within the url (i.e. anchor)

Discussion (8)2
Log in or sign up to continue

Parsing is not difficult using the $PIECE COS function:


parseURLQuery(sURL,data) PUBLIC {
  Do ##class(%Net.URLParser).Parse(sURL,.aComponents)
  Set query=aComponents("query")
  For i=1:1:$Length(query,"&") {
    Set qry=$Piece(query,"&",i),name=$Piece(qry,"=",1),value=$Piece(qry,"=",2)
    Set data(name)=value


And result:


USER>Set url=""
USER>Do parseURLQuery^test(url,.data) ZWrite data


You may also consider encoding of given page, because the URL query can be encoded and in different charset (e.g. UTF-8), so in such case you should also decode the value and translate it from given charset:

   Set data(name)=$zconvert($zconvert(value,"I","URL"),"I","UTF8")

I know it's not too difficult, just thought maybe there is an API call we can use which will do the job for us.

There are few concerns, like conversions you mentioned, duplicate properties, query order nodes, etc.


For example for string data array would be

    set url=""
    Do ##class(%Net.URLParser).Parse(url, .aComponents)
    set query=$lfs($get(aComponents("query")),"&")
    for i=1:1:$ll(query) {
        set $lb(name,value)=$lfs($lg(query,i),"=")
        set index=$order(data(name,""),-1)+1
        set data(name,index)=$g(value)
        set data(name,index,"O")=i
    zwrite data



You can use the example method (parseURLquery) that Sergei posted to get at the query (request params) information.  You might also consider applying the $zconvert code that Tomas recommended as a good practice to account for any possible encoding and or use of different character sets.   Also, Fabian was kind enough to post a link to InterSystems documentation that discusses the built-in (%Net.URLParser) Parse method - which would be useful/helpful, as well.

Also, you could use a simple javascript method to retrieve the value for a specified request parameter from a URL.  Hope this helps and Happy Coding!!!


ClientMethod getRequestParameter(sParameterName) [ Language = javascript ]
    var sRegexPattern='[\\?&]'+sParameterName+'=([^&#]*)';
    var oRegex=new RegExp(sRegexPattern);
    var aParms = oRegex.exec(window.location.href);
    if (aParms == null) {
        return "";
    } else {
        return aParms[1];