1

I have to create a RESTful service and into the GET handler I have to call a pl/sql procedure. This service can take optional parameters (e.g. http://localhost:8080/ords/Orders/getOrders?status=Pending&orderID=1). How can I use the optional parameters into the pl/sql procedure, because I need to return the data based on them?

Thank you!

1 Answer 1

0

Define parameters for your REST API.

SOURCE_TYPE => URI ACCESS_METHOD => IN

Calling the API...

enter image description here

Here's actual code, full demo:

-- Generated by ORDS REST Data Services 22.3.3.r3111929
-- Schema: ADMIN  Date: Tue Dec 20 03:39:01 2022 
--

BEGIN
  ORDS.ENABLE_SCHEMA(
      p_enabled             => TRUE,
      p_schema              => 'ADMIN',
      p_url_mapping_type    => 'BASE_PATH',
      p_url_mapping_pattern => 'admin',
      p_auto_rest_auth      => FALSE);
    
  ORDS.DEFINE_MODULE(
      p_module_name    => 'orders',
      p_base_path      => '/orders/',
      p_items_per_page => 25,
      p_status         => 'PUBLISHED',
      p_comments       => 'question for stack overflow');

  ORDS.DEFINE_TEMPLATE(
      p_module_name    => 'orders',
      p_pattern        => 'getOrders',
      p_priority       => 0,
      p_etag_type      => 'HASH',
      p_etag_query     => NULL,
      p_comments       => NULL);

  ORDS.DEFINE_HANDLER(
      p_module_name    => 'orders',
      p_pattern        => 'getOrders',
      p_method         => 'GET',
      p_source_type    => 'plsql/block',
      p_mimes_allowed  => NULL,
      p_comments       => NULL,
      p_source         => 
'begin
    -- order id
    -- status
    :output := ''Submitted status is '' || :status1 || '' and Order ID is '' || :orderid || ''.'' ;
end;');

  ORDS.DEFINE_PARAMETER(
      p_module_name        => 'orders',
      p_pattern            => 'getOrders',
      p_method             => 'GET',
      p_name               => 'response',
      p_bind_variable_name => 'output',
      p_source_type        => 'RESPONSE',
      p_param_type         => 'STRING',
      p_access_method      => 'OUT',
      p_comments           => NULL);

  ORDS.DEFINE_PARAMETER(
      p_module_name        => 'orders',
      p_pattern            => 'getOrders',
      p_method             => 'GET',
      p_name               => 'orderid',
      p_bind_variable_name => 'orderid',
      p_source_type        => 'URI',
      p_param_type         => 'INT',
      p_access_method      => 'IN',
      p_comments           => NULL);

  ORDS.DEFINE_PARAMETER(
      p_module_name        => 'orders',
      p_pattern            => 'getOrders',
      p_method             => 'GET',
      p_name               => 'status',
      p_bind_variable_name => 'status1',
      p_source_type        => 'URI',
      p_param_type         => 'STRING',
      p_access_method      => 'IN',
      p_comments           => NULL);

    
        
COMMIT;

END;

Disclaimer: I work for Oracle and am the product manager for Oracle REST Data Services.

4
  • thank you so much for your answer! however, I can't make it work. Maybe because I have a stored procedure. I wrote in p_source this 'begin PR_ORDS_GET_ORDERS(:orderid, :status); end;' and I get this error. Error report - ORA-02290: check constraint (ORDS_METADATA.REST_PARAMS_PARAM_TYPE_CK) violated ORA-06512: at "ORDS_METADATA.ORDS", line 1153 ORA-06512: at line 47 02290. 00000 - "check constraint (%s.%s) violated" *Cause: The values being inserted do not satisfy the named check *Action: do not insert values that violate the constraint.
    – alexsmt
    Commented Dec 20, 2022 at 16:23
  • The check constraint is saying your parameter isn't defined using a valid type param_type IN ( 'STRING', 'INT', 'DOUBLE', 'BOOLEAN', 'LONG', 'TIMESTAMP', 'RESULTSET' ). You having/using a stored procedure should make no difference. Commented Dec 20, 2022 at 16:34
  • 1
    thank you very much for your time and efforts! indeed, I used 'NUMBER' instead of 'INT'. I changed it and now it works. Thanks!
    – alexsmt
    Commented Dec 20, 2022 at 16:44
  • @alexsmt no worries! Good luck on your ORDS/REST API journey! Commented Dec 20, 2022 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.