Habari Web Components is a web application framework for small to medium size HTTP services, based on the Internet Direct (Indy) library.
A new demo application – included with full source code in demo/restful-crud – exposes a RESTful API which supports HTTP GET / POST / DELETE commands to read, create and delete data.
The source code below shows the configuration code for the RESTful API. Every request handler is bound to a resource address, document type (MIME type), and HTTP method.
Note that PUT is not yet implemented in the API because web browsers do not support HTTP PUT in HTML forms. For HTTP DELETE, the web interface uses a Javascript-based workaround to delete a resource.
procedure TMyRestfulComponent.Init(const Config: IWebComponentConfig); begin inherited; // always call inherited.Init // GET http://localhost/rest/persons // list all persons &Path('persons'); Produces('text/html'); GET(procedure(Request: TRequest; Response: TResponse) begin Response.ContentText := CRUDModule.GetPersons; Response.CharSet := 'UTF-8'; end); // POST http://localhost/rest/persons // add new person &Path('persons'); Produces('text/html'); POST(procedure(Request: TRequest; Response: TResponse) var Name: string; Person: TPerson; begin Name := UTF8Decode(Request.Params.Values['name']); Person := TPerson.Create(CRUDModule.NextID, Name); CRUDModule.SavePerson(Person); Response.Redirect(Request.Document); end); // PUT http://localhost/rest/persons/{id} // update person &Path('persons/{id}'); Produces('text/html'); PUT(procedure(Request: TRequest; Response: TResponse) var ID: string; begin ID := Request.Params.Values['id']; // TODO end); // DELETE http://localhost/rest/persons/{id} // delete person &Path('persons/{id}'); Produces('text/html'); DELETE(procedure(Request: TRequest; Response: TResponse) var ID: string; begin ID := Request.Params.Values['id']; CRUDModule.DeletePerson(StrToInt(ID)); end); // GET http://localhost/rest/persons/{id} // get person information &Path('persons/{id}'); Produces('text/html'); GET(procedure(Request: TRequest; Response: TResponse) var ID: string; begin ID := Request.Params.Values['id']; Response.ContentText := CRUDModule.GetPerson(StrToInt(ID)); Response.CharSet := 'UTF-8'; end); end;
Discover more from Habarisoft Blog
Subscribe to get the latest posts sent to your email.