├── .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 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
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 |
--------------------------------------------------------------------------------