Wednesday, 13 August 2014

Getting the instance URL

To get the instance URL of your APEX environment, it is not too uncommon to use OWA_UTIL.GET_CGI_ENV function calls, similar to the below block:

declare
    l_protocol varchar2(5);
    l_host varchar2(150);
    l_script varchar2(15);
    
    l_instance_url varchar2(200);
begin


    l_protocol      := owa_util.get_cgi_env('REQUEST_PROTOCOL');
    l_host          := owa_util.get_cgi_env('HTTP_HOST');
    l_script        := owa_util.get_cgi_env('SCRIPT_NAME');
    
    l_instance_url := l_protocol;
    l_instance_url := l_instance_url || '://';
    l_instance_url := l_instance_url || l_host;
    l_instance_url := l_instance_url || l_script;
    l_instance_url := l_instance_url || '/';
    
    dbms_output.put_line(l_instance_url);
  end;  

Something you might not know, is that there are a couple of helper functions in the APEX API that allow you to do exactly that.

Option 1: APEX_UTIL.HOST_URL('SCRIPT')

Reference: http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/apex_util.htm#AEAPI2312

Option 2: APEX_MAIL.GET_INSTANCE_URL()

Reference: http://docs.oracle.com/cd/E37097_01/doc/doc.42/e35127/apex_mail.htm#AEAPI29399

Option 2 seems to work even without being in the context of a HTTP session, but as per the docs: "This function requires that the instance setting Application Express Instance URL for emails is set."

No comments:

Post a Comment