2

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

2 Answers 2

2

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'

1
  • not a great example...i should have an exception handler there to catch bad INSERT attempts and return a proper response code, otherwise all errors will come back as a 500/555 Commented Jun 22, 2020 at 20:43
0

Small update. It is possible to use within your plsql/block type handler an implicit parameter (1), (2) and set the response code using :status_code with no need of custom binding just like this:

:status_code := 201;

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.