Sending HTTP POST Requests to ORDS from Swift

In this example I am referring to a RESTful web services framework created by Oracle however, you could substitute your own web server / web application framework / RESTful service as well.

Oracle REST Data Services (ORDS) sits on top of the Oracle database and allows resources in the database to be REST enabled. Once you have ORDS setup you can send HTTP requests to ORDS and define application logic using PL/SQL blocks inside of resource handlers. Resource handlers are grouped within resource templates. Templates define the base URI (i.e.: /login). Resource templates are grouped within RESTful Service Modules. Modules allow you to group your URIs using a specified "base URI" called the "URI Prefix".

Suppose we built a custom login resource handler inside our HR module in ORDS to accept HTTP POST requests by reading an email address and password from the HTTP header data. The goal is to allow users to login to a native iOS application written in Swift. This would be similar to traditional login requests sent to web applications.

The process should look something like this:

An iOS application (app) gets the ID (email address in this case) and password from the user

The app creates an HTTP post request and sends this data to ORDS

ORDS performs some application logic in PL/SQL and verifies if the login credentials are valid

In my proof of concept I decided to use OUT bind variables to return the login status / results as HTTP header data.

Here is a minimalistic example of the Swift code that sends an HTTP POST request to an ORDS resource handler called hr/login/. NB: you will not be able to run this example code by copying and pasting it as-is. You will first need to change the URL of the web application to something that is valid or by replacing the URI [workspace]/hr/login/ with the name of your own workspace and resource handler.

Note: My ORDS proof of concept for the Swift HTTP POST example code was built using a demonstration Oracle Application Express (APEX) workspace hosted at apex.oracle.com. If you don't have ORDS up and running anywhere else you can test this out by getting your own APEX workspace setup and playing with the RESTful Services module within your own workspace.

You can run this sample code in a Playground in Xcode but as of Xcode 7.3.1 and Swift 2 you need to add the following lines of code to the top of your playground.

This enables background threaded operations to be executed and results returned. Otherwise you will not see any output from the HTTP request task. In my example Swift code above the login ID and password are hardcoded as an associative array (hash -or- array of key-value pairs) but in a real application you would likely have some kind of login screen with UITextField objects for email and password input.

After Swift sends the HTTP POST request, here is an example of what my ORDS resource handler sends back to iOS as an HTTP response.

The two HTTP header variables returned from my ORDS proof of concept are api_session_id and api_status which I named arbitrarily to represent some hypothetical API and session state... assuming I was using ORDS as a backend for an iOS application.

Based on this example you should be able to create Swift code that will send data using the HTTP POST method to a web application server. Whether you are using ORDS or any other application server is up to you and the requirements for your application.

For bad passwords or invalid email addresses, here is an example of what my ORDS resource handler returns in cases where the user fails to login to the application.