oracle-apexoracle-ords

Oracle Rest Data service - customise response status code


By default all successfull request on ORDS respond with status code 200 Ok. it is well. But on occassion I need respond with on customizable error code, ie. 201 , 202 etc.

Are there any mode to respond a customizable error code on ORDS on PUT request?

regards Pedro


Solution

  • Yes, absolutely.

    Simply add a parameter called X-ORDS-STATUS-CODE, and assign it to a :bind, that you have as an OUT RESPONSE HEADER, of type INTEGER.

    Then in your POST or PUT handler code, assign the status code you want.

    :status := 201;
    

    So...

    enter image description here

    Here's the full module export -

    -- Generated by Oracle SQL Developer REST Data Services 20.2.0.147.0319
    -- Exported REST Definitions from ORDS Schema Version 20.2.0.r1611903
    -- Schema: HR   Date: Mon Jun 22 16:41:15 EDT 2020
    --
    BEGIN
      ORDS.DEFINE_MODULE(
          p_module_name    => 'status',
          p_base_path      => '/status/',
          p_items_per_page =>  25,
          p_status         => 'PUBLISHED',
          p_comments       => NULL);      
      ORDS.DEFINE_TEMPLATE(
          p_module_name    => 'status',
          p_pattern        => '201',
          p_priority       => 0,
          p_etag_type      => 'HASH',
          p_etag_query     => NULL,
          p_comments       => NULL);
      ORDS.DEFINE_HANDLER(
          p_module_name    => 'status',
          p_pattern        => '201',
          p_method         => 'POST',
          p_source_type    => 'plsql/block',
          p_items_per_page =>  0,
          p_mimes_allowed  => '',
          p_comments       => NULL,
          p_source         => 
    'declare
     new_record integer;
    begin
     insert into demo201 (column2) values (:words) returning column1 into new_record;
     commit;
     :status := 201;
    end;'
          );
      ORDS.DEFINE_PARAMETER(
          p_module_name        => 'status',
          p_pattern            => '201',
          p_method             => 'POST',
          p_name               => 'X-ORDS-STATUS-CODE',
          p_bind_variable_name => 'status',
          p_source_type        => 'HEADER',
          p_param_type         => 'INT',
          p_access_method      => 'OUT',
          p_comments           => NULL);      
    
    
      COMMIT; 
    END;
    

    Here's a somewhat better example where I've caught my exception and set the HTTP Response Status code to '400'