Building REST APIs in Pascal with Horse | APIs REST em Pascal com Horse
Bilingual post · Post bilíngue Jump to: English · Português English {#english} Building REST APIs in Pascal with Horse Pascal is not stuck in desktop forms. With Horse — a lightweight HTTP framework popular in the Delphi ecosystem — CrabPascal v2.22.0 runs real REST servers from .dpr files. No IIS, no Apache: just crab-pascal run and curl. Why Horse in CrabPascal? Horse provides routing, JSON bodies, and middleware-style handlers. CrabPascal ships RTL shims and a runtime HTTP stack so examples work out of the box: Example Port Endpoints examples/crud/ 9000 Full product CRUD examples/time-server/ 9001 Time/date ping examples/agenda/ 9000 Contact registry All runnable with the internal runtime — no gcc required. Minimal API program SimpleAPI ; uses Horse , System . JSON ; begin THorse . Get ( '/ping' , procedure ( Req : THorseRequest ; Res : THorseResponse ; Next : TNextProc ) var J : TJSONObject ; begin J := TJSONObject . Create ; J . AddPair ( 'message' , 'pong' ); Res . Send ( J . ToJSON ); end ); THorse . Listen ( 9000 ); end . Run and test: crab-pascal run SimpleAPI.dpr curl http://localhost:9000/ping Expected response: {"message":"pong"} . CRUD example from the repo The examples/crud/crud.dpr project demonstrates production-style routes: THorse . Get ( '/produtos' , procedure ( Req , Res , Next ) begin Res . Send < TJSONObject >( TProdutoService . ListarProdutos ); end ); THorse . Post ( '/produtos' , procedure ( Req , Res , Next ) var json : TJSONObject ; begin json := Req . Body < TJSONObject >; Res . Send ( TProdutoService . CriarProduto ( json . GetValue ( 'nome' ). Value , json . GetValue ( 'categoria' ). Value , StrToFloatDef ( json . GetValue ( 'preco' ). Value , 0 ), StrToIntDef ( json . GetValue ( 'estoque' ). Value , 0 ) )); end ); Start the server: cd examples/crud crab-pascal run crud.dpr Testing with curl List products: curl http://localhost:9000/produtos Create a product: curl -X POST http://localhost:9000/produtos \ -H "Content-Type: application/j