Recent

Author Topic: rest api request object should accept json string  (Read 3591 times)

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #15 on: September 22, 2022, 04:32:13 am »
please share the sample code .

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2007
  • Fifty shades of code.
    • Delphi & FreePascal
Re: rest api request object should accept json string
« Reply #16 on: September 22, 2022, 05:29:22 am »
Have you even tried one of the samples/full projects??? You just repeat saying "give code please"
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: rest api request object should accept json string
« Reply #17 on: September 22, 2022, 08:08:56 am »
Post json-content like {"title":"Aida"}
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   {$ifdef UNIX}
  5.   cthreads, cmem,
  6.   {$endif}      
  7.   fphttpapp, httpdefs, httproute, fpjson, jsonparser;
  8.  
  9. procedure route1(aReq: TRequest; aResp: TResponse);
  10. var
  11.   s:string;
  12.   D,E : TJSONData;
  13. begin
  14.    writeln(areq.Content);
  15.    D:=GetJSON(areq.Content);
  16.    E:=d.FindPath('title');
  17.    s:=E.AsString;
  18. //   aResp.SetStatus(200);
  19.    aResp.Content:='<html><body><h1>Route 1 respone:'+s+'</h1></body></html>';
  20. end;
  21.  
  22. procedure route2(aReq: TRequest; aResp: TResponse);
  23. begin
  24.   aResp.Content:='<html><body><h1>Route 2</h1></body></html>';
  25. end;
  26.  
  27. begin
  28.   HTTPRouter.RegisterRoute('/', @route1);
  29.   HTTPRouter.RegisterRoute('/2', @route2);
  30.   Application.Port := 1999;
  31.   Application.Threaded := true;
  32.   Application.Initialize;
  33.   Application.Run;
  34. end.              
  35.  

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #18 on: September 22, 2022, 09:14:29 am »
Please share me the sample code .

I will be most happy.

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #19 on: September 22, 2022, 09:32:50 am »
Thanks . It is working .

Please help me how to handle CORS and SSL in pascal.

Regards,
Prakash

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #20 on: September 22, 2022, 02:53:33 pm »
Have you even tried one of the samples/full projects??? You just repeat saying "give code please"

I am fresher in pascal because of that I am asking you the code.

I have done lot search on this. I am not getting any path for going ahead.

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #21 on: September 22, 2022, 07:36:47 pm »
Thanks to everyone .

I have used following code . It is working for SSL .
  Application.Port := 7005;
  Application.CertificateData.PrivateKey.FileName:='xyz.key';
  Application.CertificateData.Certificate.FileName:='xyz.crt';
  Application.UseSSL:= true ;


Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #22 on: September 23, 2022, 07:09:20 am »
Does anyone is having full documents.

We would like to implement form,file upload request in pascal

Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #23 on: November 08, 2022, 04:48:28 am »
{$mode objfpc}{$H+}
 
uses
  {$ifdef UNIX}
  cthreads, cmem,
  {$endif}     
  fphttpapp, httpdefs, httproute, fpjson, jsonparser;
 
procedure route1(aReq: TRequest; aResp: TResponse);
var
  s:string;
  D,E : TJSONData;
begin
   writeln(areq.Content);
   D:=GetJSON(areq.Content);
   E:=d.FindPath('title');
   s:=E.AsString;
//   aResp.SetStatus(200);
   aResp.Content:='<html><body><h1>Route 1 respone:'+s+'</h1></body></html>';
end;
 
procedure route2(aReq: TRequest; aResp: TResponse);
begin
  aResp.Content:='<html><body><h1>Route 2</h1></body></html>';
end;
 
begin
  HTTPRouter.RegisterRoute('/', @route1);
  HTTPRouter.RegisterRoute('/2', @route2);
  Application.Port := 1999;
  Application.Threaded := true;
  Application.Initialize;
  Application.Run;
end.             
 

when I run this code it is working in postman not working in browser .

I am getting cors isssue .

browse send the preflight request with "options" request .

D:=GetJSON(areq.Content); the content is nil.




Prakash

  • Full Member
  • ***
  • Posts: 169
Re: rest api request object should accept json string
« Reply #24 on: August 31, 2023, 07:20:37 pm »
Thanks Everyone .

I have created rest api with ssl . it is working .

Now I would like to implement jwt token ( creation , validation ).

How to implement jwt in lazarus pascal.

 

TinyPortal © 2005-2018