Skip to content

Instantly share code, notes, and snippets.

@Xarkam
Created April 20, 2022 11:02
Show Gist options
  • Save Xarkam/b75feaff79bda3d48a365128336091bf to your computer and use it in GitHub Desktop.
Save Xarkam/b75feaff79bda3d48a365128336091bf to your computer and use it in GitHub Desktop.
little pascal rest api
program WebRestServer;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}cthreads, cmem, {$ENDIF}
SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser,
fphttpclient, opensslsockets,Classes;
procedure jsonResponse(var aResponse: TResponse; data: String);
begin
aResponse.Content := data;
aResponse.Code := 200;
aResponse.ContentType := 'application/json';
aResponse.ContentLength := length(aResponse.Content);
aResponse.SendContent;
end;
procedure catchallEndpoint(aRequest: TRequest; aResponse: TResponse);
begin
aResponse.Content:='This end pointis not available';
aResponse.Code:=404;
aResponse.ContentType:='text/plain';
aResponse.ContentLength:=Length(aResponse.Content);
aResponse.SendContent;
end;
procedure timeEndpoint(aRequest: TRequest; aResponse: TResponse);
var
jObject : TJSONObject;
begin
jObject := TJSONObject.Create;
try
jObject.Strings['time'] := TimeToStr(Time);
jsonResponse(aResponse, jObject.AsJSON);
finally
jObject.Free;
end;
end;
procedure greetingEdnpoint(aRequest: TRequest; aResponse: TResponse);
var
jObject : TJSONObject;
begin
jObject := TJSONObject.Create;
try
jObject.Strings['greeting'] := 'Hello, ' + aRequest.RouteParams['name'];
aResponse.Content := jObject.AsJSON;
aResponse.Code := 200;
aResponse.ContentType := 'application/json';
aResponse.ContentLength := length(aResponse.Content);
aResponse.SendContent;
finally
jObject.Free;
end;
end;
procedure readJsonPostsEndpoint(aRequest: TRequest; aResponse: TResponse);
var
jObject : TJSONObject;
jArr: TJSONArray;
jEnum: TJSONEnum;
response: String;
begin
jArr:=TJSONArray.Create;
try
response:=TFPHTTPClient.SimpleGet('https://jsonplaceholder.typicode.com/posts');
WriteLn(response);
jArr:=GetJSON(response) as TJSONArray;
for jEnum in jArr do begin
jObject:=jEnum.Value as TJSONObject;
WriteLn();
WriteLn('::' + jObject.Strings['title'] + ' ::');
WriteLn(jObject.FindPath('body').AsString);
end;
finally
jArr.Free;
end;
end;
procedure readJsonUsersEndpoint(aRequest: TRequest; aResponse: TResponse);
var
jObject : TJSONObject;
jArr: TJSONArray;
jEnum: TJSONEnum;
response: String;
begin
jArr:=TJSONArray.Create;
try
response:=TFPHTTPClient.SimpleGet('https://jsonplaceholder.typicode.com/users');
WriteLn(response);
jArr:=GetJSON(response) as TJSONArray;
for jEnum in jArr do begin
jObject:=jEnum.Value as TJSONObject;
WriteLn();
WriteLn('::' + jObject.Strings['name'] + ' ::');
WriteLn(jObject.FindPath('email').AsString);
WriteLn(jObject.Objects['address'].Strings['street']);
WriteLn(jObject.Objects['address'].Strings['city']);
WriteLn(jObject.Objects['address'].Strings['zipcode']);
end;
finally
jArr.Free;
end;
end;
begin
Application.Port := 9081;
HTTPRouter.RegisterRoute('/time', rmGet, @timeEndpoint);
HTTPRouter.RegisterRoute('/greeting/:name', @greetingEdnpoint);
HTTPRouter.RegisterRoute('/catchall', rmall, @catchallEndpoint, true);
HTTPRouter.RegisterRoute('/posts', rmget, @readJsonPostsEndpoint);
HTTPRouter.RegisterRoute('/users', rmget, @readJsonUsersEndpoint);
Application.Threaded := true;
Application.Initialize;
WriteLn('Server is ready at http://localhost:' + IntToStr(Application.Port));
Application.Run;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment