Page History
...
Method 1: Simple Automation
For simple database related resources, the GetDatabaseItem service (a member of the HTTP_Resource_Services module) can be used with minimal code. Just get the Key ID from the prepopulated EndpointSegment variable and pass in the name of the database table:
Code Block | ||
---|---|---|
| ||
API contacts.ID.GET KeyID = EndpointSegment HTTP_Resource_Services('GetDatabaseItem', 'CONTACTS', '', KeyID) end api |
This produces the following resource object:
No Format |
---|
{ "address":"6649 N Blue Gum St", "birthdate":"", "city":"New Orleans", "company":"Benton, John B Jr", "county":"Orleans", "email":"jbutt@gmail.com", "first_name":"James", "last_name":"Butt", "notes":"", "picture":"\\WebAppData\\ContactPictures\\1.jpeg", "state":"LA", "url":"http://www.bentonjohnbjr.com", "zip":"70116", "phone":[ { "phone_number":"(504) 621-8927", "phone_type":"Phone 1" }, { "phone_number":"(504) 845-1427", "phone_type":"Phone 2" } ] } |
One of the drawbacks of calling the GetDatabaseItem service is that property names are formatted with underscores (i.e., how they appear in the dictionary) rather than as camel case (which is the conventional format for JSON objects). Another drawback is that the GetDatabaseItem service attempts to return all column data (both physical and calculated). This might be undesirable if some of the data is meaningless to the client. Consider the picture property in the above resource object. It references an image file stored locally on the server, which has no value to the client. To resolve both of the above items, we will take advantage of the optional ColumnNames and ItemArrayLabel arguments. To keep our sample code simple and concise, we will limit our resource to just the FIRST_NAME, LAST_NAME, ADDRESS, CITY, STATE, and ZIP database columns:
Code Block | ||
---|---|---|
| ||
API contacts.ID.GET KeyID = EndpointSegment ColumnNames = 'FIRST_NAME' : @FM : 'LAST_NAME' : @FM : 'ADDRESS' : @FM : 'CITY' : @FM : 'STATE' : @FM : 'ZIP' ItemArrayLabel = 'firstName' : @FM : 'lastName' : @FM : 'address' : @FM : 'city' : @FM : 'state' : @FM : 'zipCode' HTTP_Resource_Services('GetDatabaseItem', 'CONTACTS', '', KeyID, ColumnNames, ItemArrayLabel) end api |
Our resource object now appears like this:
No Format |
---|
{ "address": "6649 N Blue Gum St", "city": "New Orleans", "firstName": "James", "lastName": "Butt", "state": "LA", "zipCode": "70116" } |
Pros:
- Simple to call.
- It creates the response body, content-type header, and status code automatically.
Cons:
- Deprecated (but there are no plans to remove it).
- HATEOAS support is limited.
- Cannot include the Key ID within the resource object.