Get path param value into ORDS prehook

Viewed 327

Is there a way to get pathparam from url with get_cgi_env? Example: https://clientes/{codigo}. I would like to get the value from :codigo pathparam.

Into handler GET, POST, PUT, etc. on endpoint it is possible to get the pathparam value but it's not clear how can to be done into ORDS prehook.

2 Answers

I have this function to get the complete url - the "QUERY_STRING" portion should give you the parameters

  FUNCTION request_url RETURN VARCHAR2
  IS
    l_url VARCHAR2(1024);
  BEGIN
    l_url := owa_util.get_cgi_env('SERVER_NAME') ||':' ||
           owa_util.get_cgi_env('SERVER_PORT') ||
           owa_util.get_cgi_env('SCRIPT_NAME') ||
           owa_util.get_cgi_env('PATH_INFO') ||
           owa_util.get_cgi_env('QUERY_STRING');
    RETURN l_url;
  EXCEPTION WHEN VALUE_ERROR THEN
    RETURN 'unable to retrieve request_url';
  END request_url;

You can access all the CGI variables available via owa.cgi_var_name and owa.cgi_var_val (owa.num_cgi_vars for the count).

I have this running in my preHook:

pragma autonomous_transaction;
l_obj  json_object_t := json_object_t();
l_json blob;
  begin
    for i in 1 .. owa.num_cgi_vars loop
      l_obj.put(owa.cgi_var_name(i), owa.cgi_var_val(i));
    end loop;
    l_json := l_obj.to_blob;
    insert into rest_access
      (id, created, cgi_variables, current_schema)
    values
      (s_rest_access.nextval, sysdate, l_json, sys_context('userenv', 'current_schema'));
    commit;

Lots of values that you have access to, including parameters.

To see a list of parameters after running it and logging requests, simply run

select *
  from json_table((select json_dataguide(cgi_variables, dbms_json.format_flat) from rest_access),
                  '$[*]' columns( --
                          opath varchar2(200) path '$."o:path"',
                          otype varchar2(40) path '$.type',
                          olength number path '$."o:length"')) j;
Related