├── .gitignore ├── LICENSE ├── Lazarus.png ├── README.md └── source ├── TinyApiExample.lpi └── TinyApiExample.pas /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.lps 3 | source/bin 4 | source/backup 5 | source/lib 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Marcus Fernstrom 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Lazarus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MFernstrom/FreePascal-API-server-example/c3966104bfadb7a9fb1f80612d4db7e9e4982796/Lazarus.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | FreePascal / Lazarus example of how to create a REST API using the Lazarus FPHTTPServer. 4 | 5 | Included 6 | 7 | * Several endpoints 8 | * JSON data generation 9 | * Custom content type header 10 | * Default handler 11 | -------------------------------------------------------------------------------- /source/TinyApiExample.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | <UseAppBundle Value="False"/> 16 | <ResourceType Value="res"/> 17 | </General> 18 | <BuildModes Count="1"> 19 | <Item1 Name="Default" Default="True"/> 20 | </BuildModes> 21 | <PublishOptions> 22 | <Version Value="2"/> 23 | <UseFileFilters Value="True"/> 24 | </PublishOptions> 25 | <RunParams> 26 | <FormatVersion Value="2"/> 27 | <Modes Count="0"/> 28 | </RunParams> 29 | <Units Count="1"> 30 | <Unit0> 31 | <Filename Value="TinyApiExample.pas"/> 32 | <IsPartOfProject Value="True"/> 33 | </Unit0> 34 | </Units> 35 | </ProjectOptions> 36 | <CompilerOptions> 37 | <Version Value="11"/> 38 | <PathDelim Value="\"/> 39 | <Target> 40 | <Filename Value="bin/$(TargetOs)/TinyApiExample"/> 41 | </Target> 42 | <SearchPaths> 43 | <IncludeFiles Value="$(ProjOutDir)"/> 44 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 45 | </SearchPaths> 46 | </CompilerOptions> 47 | <Debugging> 48 | <Exceptions Count="3"> 49 | <Item1> 50 | <Name Value="EAbort"/> 51 | </Item1> 52 | <Item2> 53 | <Name Value="ECodetoolError"/> 54 | </Item2> 55 | <Item3> 56 | <Name Value="EFOpenError"/> 57 | </Item3> 58 | </Exceptions> 59 | </Debugging> 60 | </CONFIG> 61 | -------------------------------------------------------------------------------- /source/TinyApiExample.pas: -------------------------------------------------------------------------------- 1 | program TinyApiExample; 2 | 3 | { 4 | Copyright 2018, Marcus Fernstrom 5 | License MIT 6 | } 7 | 8 | {$mode objfpc}{$H+} 9 | 10 | uses 11 | {$IFDEF UNIX}cthreads, cmem,{$ENDIF} 12 | SysUtils, fphttpapp, httpdefs, httproute, fpjson; 13 | 14 | procedure jsonEndpoint(aRequest : TRequest; aResponse : TResponse); 15 | var 16 | jObject : TJSONObject; 17 | begin 18 | jObject := TJSONObject.Create; 19 | try 20 | jObject.Booleans['success'] := true; 21 | jObject.Strings['data'] := 'This is a JSON object'; 22 | jObject.Integers['numbers'] := 12345; 23 | 24 | aResponse.Content := jObject.AsJSON; 25 | aResponse.ContentType := 'application/json'; 26 | aResponse.SendContent; 27 | finally 28 | jObject.Free; 29 | end; 30 | end; 31 | 32 | procedure textEndpoint(aRequest : TRequest; aResponse : TResponse); 33 | begin 34 | aResponse.Content := 'This is the default response if no other routes match.'; 35 | aResponse.ContentType := 'text/plain'; 36 | aResponse.SendContent; 37 | end; 38 | 39 | begin 40 | Application.Port := 9080; 41 | HTTPRouter.RegisterRoute('/json', @jsonEndpoint); 42 | HTTPRouter.RegisterRoute('/text', @textEndpoint, true); 43 | Application.Threaded := true; 44 | Application.Initialize; 45 | WriteLn('Server is ready at http://localhost:' + IntToStr(Application.Port)); 46 | Application.Run; 47 | end. 48 | 49 | --------------------------------------------------------------------------------