Appery.io Binary data in API Express (Generated services)



API Express provides powerful and flexible mechanism of working with binary data.
API Express uses JSON format so to work with binary data it is used Base64 format (you can read more about that by following link https://en.wikipedia.org/wiki/Base64 )

For testing you can use Base64 Converter to convert ordinary string to string in Base64 format and vise versa.

Binary data in Generated Services
API Express provides embedded support of Binary for Generated services.

  1. Fields with type BLOB in generated services are considered as Binary
  2. Operations get and list will convert value of this field into Base64 String
  3. Operations create and update will accept String in Base64 format and API Express will save converted binary into database.

To understand better how it work let’s consider example
Relational database has table files with columns id, name and content
id - unique identifier of files name - file name content - file binary content
Following DDL scripts can be used for 

MySQL
CREATE TABLE files
( id INT NOT NULL auto_increment PRIMARY KEY, name VARCHAR(64) NOT NULL, content BLOB NOT NULL )

Postgresql
CREATE TABLE files
( id SERIAL PRIMARY KEY, name VARCHAR(64) NOT NULL, content BYTEA NOT NULL )

MSSQL
CREATE TABLE files
( id INT PRIMARY KEY IDENTITY, name VARCHAR(64) NOT NULL, content VARBINARY NOT NULL )

Oracle

CREATE TABLE files ( id INT PRIMARY KEY, name VARCHAR(64) NOT NULL, content BLOB NOT NULL )

It is assumed that already exist Database Connection to Relational database.
To work with table let’s create API Express Generated service


On Test page we can check how our services are working
1. Create record in files table.
name of our file will be test.txt
file will contain only sentence test message in Base64 format it is IHRlc3QgbWVzc2FnZQ== Request payload { "name": "test.txt", "content": "IHRlc3QgbWVzc2FnZQ==" } Response of operation create { "content": "IHRlc3QgbWVzc2FnZQ==", "id": 1, "name": "test.txt" }


If to take a look on this record in database client we would see that in database sentence test message was saved
2. Get record from files table.
Operation find will return just created record if to pass file name test.txt in query { "name": "test.txt" } Response of operation find is [{ "content": "IHRlc3QgbWVzc2FnZQ==", "id": 1, "name": "test.txt" }]


Comments