Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, David.

Asked: October 30, 2019 - 8:30 am UTC

Last updated: November 07, 2019 - 3:02 am UTC

Version: 5.1.1

Viewed 1000+ times

You Asked

Hi,

Is there any way to programmatically (in PL/SQL) identify the currently invoked RESTful service that is executing the PL/SQL code ?

For example, I have a RESTful service that calls PL/SQL code and would like to use the calling service name for conditional logic etc. From within that PL/SQL code, is there any way to identify what parent RESTful service called it, similar to the way you can access APEX variables inside database PL/SQL code ?

Thanks,
David.

and Connor said...

I don't think so without doing it explicitly yourself.

I tried tracing the ORDS sessions and also dumping out context variables, and got nothing of value pertaining the call.

You could edit the REST definitions to set a context for the duration of the call, eg

Before

begin
    update emp set ename = :ename, job = :job, hiredate = :hiredate
    where empno = :id;
    :status := 200;
    :location := :id;
exception
    when others then
        :status := 400;
end;


After

begin
    dbms_session.set_context('MYCTX','RESTPT','/hr/ermployees/:id/PUT');
    update emp set ename = :ename, job = :job, hiredate = :hiredate
    where empno = :id;
    :status := 200;
    :location := :id;
exception
    when others then
        :status := 400;
end;

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

owa_util

Mikhail Velikikh, November 06, 2019 - 11:13 am UTC

I would use OWA_UTIL.GET_CGI_ENV('SCRIPT_NAME') for this task.

BEGIN
  ORDS.define_service(
    p_module_name    => 'test-cgi-env',
    p_base_path      => 'test-cgi-env/',
    p_pattern        => 'test/',
    p_method         => 'GET',
    p_source_type    => ORDS.source_type_plsql,
    p_source         => q'#begin
                             owa_util.mime_header('text/plain');
                             owa_util.print_cgi_env;
                           end;#',
    p_items_per_page => 0);

  COMMIT;
END;
/

curl produces:

curl http://localhost:8080/ords/hr/test-cgi-env/test/

..skip..
SCRIPT_NAME = /ords/hr/test-cgi-env/test<br />
..skip..
X-APEX-PATH = test-cgi-env/test/<br />
..skip..
Connor McDonald
November 07, 2019 - 3:02 am UTC

Awesome stuff!

More to Explore

DBMS_SESSION

More on PL/SQL routine DBMS_SESSION here