Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a REST web service in Delphi 2009 using IIS

Is is possible to create a REST web service in Delphi 2009 which runs on IIS? I have looked at the web broker and web snap web server applications but so far cannot figure it out. If it is possible how would I go about getting it to work?

like image 331
There is no spoon Avatar asked May 18 '11 02:05

There is no spoon


1 Answers

REST web services are super simple in Delphi...too simple in fact as sometimes it's glossed over and hidden in complex WebSnap demos. You can use Web broker, WebSnap, Indy, Synapse or other generic HTTP server components. I wouldn't use WebSnap on your first RESTful outing. I'd stick with WebBroker or, even easier, Indy.

File->New->Other WebBroker->Web Server Application

You have choices of a ISAPI DLL or Web App Debugger... for your first one, I recommend a WAD application. (In fact, any web application should start with a WAD for the server, and then create a client tester application to make your debugging much easier later.) If you select WAD, then give it a name - like MyFirstREST. This will create a project that has a default web module that responds with a simple HTML doc to any request.

Right click the web module, click on the Action Editor pop-up menu item, and create your RESTful commands, via custom PathInfo settings. Then you simply define the events to respond to these commands.

For Indy, just start a new Windows Service Application and drop a TidHTTPServer component on the service datamodule. Then define an OnCommandGET for simple HTTP GET requests (http://yourserver/customer?id=1) Also define an OnCommandOther for POST requests.

There is a TIdHTTPResponseInfo and a TIdHTTPResponseInfo sent into the event...just look at the QueryParams for a GET to extract the parameters passed in and populate the response's ContentStream (or ContentText), along with the ContentType.

Many tutorials on REST implementations can be found online - one is from IBM: http://www.ibm.com/developerworks/webservices/library/ws-restful/

Typically you use RESTful servers to work with XML requests and responses but there's no restrictions on how you implement a server. It all depends on how the clients expectations. If you are controlling the client side as well, then you can even pass back objects back and forth by serializing TObject descendants.

like image 62
Darian Miller Avatar answered Nov 09 '22 10:11

Darian Miller