Instantly expose a data table as JSON/REST with Delphi.

RESTDebug

[UPDATE] For a more complete example of building a RESTful service client with Delphi, and a RESTful JSON service using PHP, check out my “Brutally Roll Your Own Backend” video series!

Since my skill sprint on JSON/REST services in Delphi and C++ builder back in February, one thing has been nagging on my mind. The code that I wrote for that skill sprint was written specifically to expose one particular database table, but it was almost generic enough that it could expose any database table. So why don’t I write a component for exposing tables as JSON/REST endpoints?

Well, for the past two or three days I’ve been working on just such a component, and I’d now like to demonstrate for you, just how easy it can be to create a JSON/REST service application using it. The server side component, when placed into a WebBroker application module, automatically adds an endpoint, and you simply need to bind the component up to a database connection to make it function.

This video demonstrates how to install and use the component…

DISCLAIMER

I must apologize for needing a disclaimer, but I’m sure you’ll understand why it’s necessary…

Before I hand you the source code for this component, and demonstrate it’s use, I’d like to make one thing very clear. I wrote this component (actually two components) for no other purpose than to demonstrate that it could be done. I wouldn’t want you to think that this code is in any way safe for commercial use. I do provide these components free of charge, and welcome you to use them for any purpose, however, as it is not commercial grade code I will not be held responsible if you use it and incur damages. I would be happy to entertain volunteers that wish to review and enhance the code, add unit tests, add injection protection, authentication and so on, however, I will not personally be investing in this code any further. Okay?

The source:

Okay, so the source code is may be downloaded here:  JCRUD

Thanks for reading!

 

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

7 Responses

  1. diwakoe@gmail.com says:

    Hi,

    I’m trying to create small application using 1 table as your short tutorial, server running tested with browser 3 record show up, but when I try to connect the client always get error “no peer with the interface with guid {xxxx}”.

    Is there any hints for this error?

    Thanks,
    Tegoeh

    • I really have no idea where this error is coming from, and suspect it’s somewhere between FireDAC and the Database. I’m curious which database engine you’re using?
      In any case, there is a possibility that you’ve run into a situation in which the ICRUD interface requires a GUID, so you could try this…
      Open jcrud.crud.pas and scroll down to line 103. Press [CRTL]+[SHIFT] [G], which should inject a GUID into the code. The do a clean and build and test again.

      • diwakoe@gmail.com says:

        Hi,

        I’m using Firedac Connection with Postgresql as database, I already done inject the GUID clean and build the code but error still show up.

        Error said “Module Load: IPHLPAPI.DLL. No Debug Info. Base Address: $72CD0000. Process LokClient.exe (8608)
        First chance exception at $7523A832. Exception class EIPAbstractError with message ‘No peer with the interface with guid {E0125434-B354-482C-BD99-7E61623721FF} has been registered’. Process LokClient.exe (8608)”

        Any comment is appreciated.

        Thanks,
        Tegoeh

  2. ehabko_2007@yahoo.com says:

    really good component.

    I have a qustion about , how can i make a login form useing this component ?
    i create another table in DB and called it ‘Login’ and JCRUDRESTFireDACServer (Endpoint (login) and key field (ID)) >> working good at webbrowser
    in Client
    i add another TJCRUDRestClientDataset (Endpoint (login))
    I insert this code
    if Login.Active then begin
    Login.Insert;
    Login.FieldByName[‘ID’] :=Edit1.Text;
    Login.FieldByName[‘User’] :=Edit2.Text;
    showmessage (‘Done’);
    Login.Post;

    end;

    And I show this message :
    —————————
    Error
    —————————
    HTTP/1.1 500 Internal Server Error.
    —————————
    OK
    —————————

    • If this is working in your web browser, but not in the application, the most likely cause is an incorrect field in the client.
      Many database fields are case sensitive, so please check the case of your fields within FieldByName[] so that they match the case of the same fields in the database.

  1. 2017-09-29

    […] Instantly expose a data table as JSON/REST with Delphi. – Mar 13, 2016. Since my skill sprint on JSON/REST services in Delphi and C++ builder back in. table, but it was almost generic enough that it could expose any database table. to connect the client always get error “no peer with the interface with guid {xxxx }”. end;. And I show this message : ————————— Error […]

  2. 2017-10-22

    […] Instantly expose a data table as JSON/REST with Delphi. – Mar 13, 2016. Since my skill sprint on JSON/REST services in Delphi and C++ builder back in. table, but it was almost generic enough that it could expose any database table. to connect the client always get error “no peer with the interface with guid {xxxx }”. end;. And I show this message : ————————— Error […]

Leave a Reply