Starting to use REST services in Apex can be a little confusing but hopefully by the end of this post you will feel comfortable creating your own REST services and doing something productive with them.

Let’s start by creating a few database objects:

Database Objects

CREATE TABLE  "CONTACTS" 
   ("CONTACT_ID" NUMBER, 
	"NAME" VARCHAR2(100), 
	"EMAIL" VARCHAR2(100), 
	"CREATION_DATE" DATE
   )
CREATE SEQUENCE   "CONTACTS_SEQ"  
MINVALUE 1 
MAXVALUE 9999999999999999999999999999 
INCREMENT BY 1 
START WITH 1 CACHE 10 

Create POST REST service in Apex

Go to SQL Workshop and click the RESTful services section

Select RESTful Services

Next, set-up your module and POST handler similar to the screenshot below

Module Set-Up

Source

declare 
l_contact_id number;
begin
l_contact_id := contacts_seq.nextval;
insert into contacts
(contact_id,
name,
email,
creation_date)
values
(l_contact_id,
/*Bind variables to pass data to the REST service*/
:name, 
:email,
sysdate);
end;

Save your work and click the POST handler that got created on save.
Make sure that the “Requires Secure Access” is set to no. If set to yes you will be required to serve over an HTTPS: connection.
Now click on the “Create Parameter” button to set the values for the bind variables in the source above. Set-up should look similar to the screenshot below.

Create parameter

Create a similar parameter for the email bind variable and save your work.
That’s it now you have a fully functioning REST service ready to be tested. While that’s all good and fine how do you go about testing your POST service?

I use POSTMAN to test until I am comfortable that the POST action is functioning correctly. The set-up and use of POSTMAN is out of scope for this article but if you have any questions leave a comment and I will follow-up.
The main thing when testing with a tool like POSTMAN or doing an AJAX POST call from a web page is that you get the syntax of the URL correct and set the bind variables correctly in the HTTP header.
If using Oracle provided workspace your URL for testing should look similar to this.

https://apex.oracle.com/pls/apex/{YOUR WORKSPACE NAME HERE}/contacts/createOneContact/

Here is an example of how a POSTMAN call might look

POSTMAN

You could also create an AJAX call with jQuery and attach it to any event on a web page(even a localhost page). This opens up a lot of possibilities to be able to use the Oracle database as the main data store but distribute access across many different platforms.

$("#createContact"/*click event handler id*/).click(function(){
//Get value from input items on a web page
var contactName = $('#contactName').val();
var contactEmail = $('#contactEmail').val();
var restUrl = "https://apex.oracle.com/pls/apex/{YOUR WORKSPACE NAME HERE}/contacts/
createOneContact/"; $.ajax({ url: restUrl, type: 'post', headers: {"NAME": contactName, "EMAIL": contactEmail} }); //Clear the input items on click $('#contactName').val(''); $('#contactEmail').val(''); });

Hopefully you have a better understanding of the basics of creating a RESTful service with Oracle Apex. My next post will create a GET handler to consume the data we just learned how to post.


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *