Recent

Author Topic: rest api server  (Read 976 times)

Packs

  • Sr. Member
  • ****
  • Posts: 409
rest api server
« on: February 29, 2024, 06:29:22 am »
I have created rest api server with endpoint .

This end point should accept json object

I have tried to access through ContentFields .it is not working .

for nCtr := 0 to req.ContentFields.Count - 1 do
        begin
          Frmmain.Memo1.Lines.Add('pdf file' + req.ContentFields[nCtr]);
        end;

TRon

  • Hero Member
  • *****
  • Posts: 3788
Re: rest api server
« Reply #1 on: February 29, 2024, 06:32:34 am »
I have created rest api server with endpoint .

This end point should accept json object

I have tried to access through ContentFields .it is not working .

for nCtr := 0 to req.ContentFields.Count - 1 do
        begin
          Frmmain.Memo1.Lines.Add('pdf file' + req.ContentFields[nCtr]);
        end;
Dear Prakash,

There isn't even a question in there...  :-X
I do not have to remember anything anymore thanks to total-recall.

Packs

  • Sr. Member
  • ****
  • Posts: 409
Re: rest api server
« Reply #2 on: February 29, 2024, 06:33:57 am »
Code: Pascal  [Select][+][-]
  1. procedure hallticket_rpt(req: TRequest; res: TResponse);
  2. var
  3.   dm_hallticket: Tdm_hallticket;
  4.   error, spdffile, sjsonfile: string;
  5.   nCtr: integer = 0;
  6. begin
  7.   dm_hallticket := Tdm_hallticket.Create(nil);
  8.   try
  9.     try
  10.  
  11.       if req.Method = 'POST' then
  12.       begin
  13.  
  14.         Frmmain.Memo1.Clear;
  15.         //ARequest.ContentFields.Values['Name'];
  16.  
  17.  
  18.         for nCtr := 0 to req.FieldCount - 1 do
  19.         begin
  20.           Frmmain.Memo1.Lines.Add('field name : ' + req.FieldNames[nCtr]);
  21.         end;
  22.  
  23.         for nCtr := 0 to req. - 1 do
  24.         begin
  25.           Frmmain.Memo1.Lines.Add('field name : ' + req.FieldNames[nCtr]);
  26.         end;
  27.  
  28.         for nCtr := 0 to req.ContentFields.Count - 1 do
  29.         begin
  30.           Frmmain.Memo1.Lines.Add('pdf file' + req.ContentFields[nCtr]);
  31.         end;
  32.  
  33.         //Frmmain.Memo1.Lines.Text := req.ContentFields.Values['pdffile'];
  34.  
  35.         spdffile := req.ContentFields.Values['pdffile'];
  36.         sjsonfile := req.ContentFields.Values['jsonfile'];
  37.  
  38.         Frmmain.Memo1.Lines.Add('pdf file' + spdffile);
  39.         Frmmain.Memo1.Lines.Add('json file' + sjsonfile);
  40.         if spdffile = EmptyStr then
  41.         begin
  42.           JsonResponse(res, 'Pdf file not found', 500);
  43.           Exit;
  44.         end;
  45.  
  46.         if sjsonfile = EmptyStr then
  47.         begin
  48.           debugln(MY_LOG_GROUP, ['jsonfile : ', sjsonfile]);
  49.           JsonResponse(res, 'json file not found', 500);
  50.           Exit;
  51.         end;
  52.  
  53.         dm_hallticket.Pdffilename := spdffile;
  54.         dm_hallticket.Jsonfile := sjsonfile;
  55.         dm_hallticket.load_jsonfile;
  56.         dm_hallticket.print_hallticket;
  57.         jsonResponse(res, 'true');
  58.  
  59.       end;
  60.     except
  61.       on E: Exception do
  62.       begin
  63.         error := '{"error": "' + E.Message + '"}';
  64.         JsonResponse(res, error, 500); // Internal Server Error
  65.       end;
  66.     end;
  67.   finally
  68.     dm_receipts.Free;
  69.   end;
  70. end;
  71.  
  72.  

and from postman sending

{
"pdffile":  "hallticket.pdf",
"jsonfile": "1002_hallticket.json"
}

how to access the json object from request

Packs

  • Sr. Member
  • ****
  • Posts: 409
Re: rest api server
« Reply #3 on: February 29, 2024, 07:10:33 am »
it is solved

Code: Pascal  [Select][+][-]
  1. D := GetJSON(req.Content);
  2.         spdffile := D.FindPath('pdffile').AsString;
  3.         sjsonfile := D.FindPath('jsonfile').AsString;  
  4.  

 

TinyPortal © 2005-2018