Jump to content

Using Unigui Rest Full Server


pro_imaj

Recommended Posts

Hi,

Can I use it as Unigui Rest Full.

So like this;
I will send the following 1.json format as Post method to http://localhost:8077/user link.

2. I will send it as a response in json format, can this be done?

1. Json
{
   "username": "a",
   "Password": "123"
}

2.Json
{
   "Name": "bbbb",
   "Mission": "pppppp"
}


*I tried to do a lot of reasoning and examples but as a general problem. When I type http://localhost:8077/user I see the following error on the screen.

Invalid session or session Timeout. (Invalid URI: /user )

Is there a solution
Thanks

Link to comment
Share on other sites

16 minutes ago, pro_imaj said:

Hi,

Can I use it as Unigui Rest Full.

So like this;
I will send the following 1.json format as Post method to http://localhost:8077/user link.

2. I will send it as a response in json format, can this be done?

1. Json
{
   "username": "a",
   "Password": "123"
}

2.Json
{
   "Name": "bbbb",
   "Mission": "pppppp"
}


*I tried to do a lot of reasoning and examples but as a general problem. When I type http://localhost:8077/user I see the following error on the screen.

Invalid session or session Timeout. (Invalid URI: /user )

Is there a solution
Thanks

I have prevented the error from occurring as follows, but I cannot see the JSON value sent with CustomBody in ARequestInfo. How can I get the incoming CustomBody value.

 

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin
  if ARequestInfo.URI = '/user' then
  begin

    AResponseInfo.ContentText :=
      '{"Name": "bbbb","Mission": "pppppp" }'  ;
    AResponseInfo.ResponseNo := 200;
    AResponseInfo.WriteContent;
    Handled := True;

  end;


end;

 

Link to comment
Share on other sites

This is how I return a PDF document

        AResponseInfo.ContentType:= 'application/pdf';
        AResponseInfo.ContentStream:= TFileStream.Create(FullyQualifiedPDF, fmOpenRead or fmShareCompat);
        AResponseInfo.ContentLength:= AResponseInfo.ContentStream.Size;
        AResponseInfo.WriteHeader;
        AResponseInfo.WriteContent;
        AResponseInfo.ContentStream.Free;
        AResponseInfo.ContentStream:= nil;
        AResponseInfo.ResponseNo:= 200;
        AResponseInfo.CloseConnection:= True;
        AResponseInfo.CloseSession;
        Handled:= True;

 

Link to comment
Share on other sites

11 hours ago, andyhill said:

This is how I return a PDF document

        AResponseInfo.ContentType:= 'application/pdf';
        AResponseInfo.ContentStream:= TFileStream.Create(FullyQualifiedPDF, fmOpenRead or fmShareCompat);
        AResponseInfo.ContentLength:= AResponseInfo.ContentStream.Size;
        AResponseInfo.WriteHeader;
        AResponseInfo.WriteContent;
        AResponseInfo.ContentStream.Free;
        AResponseInfo.ContentStream:= nil;
        AResponseInfo.ResponseNo:= 200;
        AResponseInfo.CloseConnection:= True;
        AResponseInfo.CloseSession;
        Handled:= True;

 

I can do this myself. What I can't do is read the incoming JSON value. I can't see the incoming Json value.

Link to comment
Share on other sites

4 hours ago, pro_imaj said:

I can do this myself. What I can't do is read the incoming JSON value. I can't see the incoming Json value.

Hello, I use some kind of REST server.

here is how to send data from Client: 

http://YOURSERVER:PORT/JSON='  +  EncodeBase64 ('ALL_JSONDATA_LIKE_STRING');

 

here is how to read data REST server :

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin

//Check if is JSON
if (POS ('/JSON',ARequestInfo.URI) > 0)
then begin      
      AResponseInfo.ResponseNo := 200;
      AResponseInfo.ContentEncoding := '1251';

      ARequestInfo.Document := mmReadDocument (1,Copy (ARequestInfo.Document,Length ('/JSON=')+1,Length (ARequestInfo.Document))); //// DO YOUR REST STUFF
      AResponseInfo.ContentText := ARequestInfo.Document;
      AResponseInfo.WriteContent;
      Handled := True;
      AResponseInfo.CloseSession;
end;
end;

 

How to process readded data REST server:

unit MainModule;

function mmReadDocument (iType: Integer; sDocument: String): String;

var

sDataList : TStringLIst;

begin

TRY

sDataList := TStringLIst.Create;

sDataList.DelimitedText := DecodeBase64 (sDocument);

FINALLY

sDataList.Clear;

sDataList.Free;

END;

end;

 

 

Link to comment
Share on other sites

On 3/4/2023 at 2:50 PM, irigsoft said:

Hello, I use some kind of REST server.

here is how to send data from Client: 

http://YOURSERVER:PORT/JSON='  +  EncodeBase64 ('ALL_JSONDATA_LIKE_STRING');

 

here is how to read data REST server :

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
begin

//Check if is JSON
if (POS ('/JSON',ARequestInfo.URI) > 0)
then begin      
      AResponseInfo.ResponseNo := 200;
      AResponseInfo.ContentEncoding := '1251';

      ARequestInfo.Document := mmReadDocument (1,Copy (ARequestInfo.Document,Length ('/JSON=')+1,Length (ARequestInfo.Document))); //// DO YOUR REST STUFF
      AResponseInfo.ContentText := ARequestInfo.Document;
      AResponseInfo.WriteContent;
      Handled := True;
      AResponseInfo.CloseSession;
end;
end;

 

How to process readded data REST server:

unit MainModule;

function mmReadDocument (iType: Integer; sDocument: String): String;

var

sDataList : TStringLIst;

begin

TRY

sDataList := TStringLIst.Create;

sDataList.DelimitedText := DecodeBase64 (sDocument);

FINALLY

sDataList.Clear;

sDataList.Free;

END;

end;

 

 

 

Hello,

Thank you for the answer.

I implemented your answer, but rest debugger does not return the correct result. I added the code you wrote into a small project, you can find the example in the attachment.

While sending the data, I want to send it as below with rest debugger.

URL http://localhost:8077/JSON

Conten Type : { "Username": "test01", "Password": "test01"}

image.png

Rest_Full.zip

Link to comment
Share on other sites

1 hour ago, pro_imaj said:

Hi, Thanks.

I was able to compile the project. But I can not get any results, what I'm doing wrong, I added the project sample.

REST Server.zip 2.93 MB · 1 download

Maybe You have problem with TNetEncoding.Base64.Encode and decoding , i am not sure.

Can you try send request from RESTDebugger to Exe compiled by me.

and see in Right memo, what result will show.

Send request by this way direct in Browser: http://yourserver:port/JSON=EncodedBase64 (JSONdata).

all must be in URL.

Try to encode data with this: https://www.base64encode.org/

Link to comment
Share on other sites

1 hour ago, irigsoft said:

Maybe You have problem with TNetEncoding.Base64.Encode and decoding , i am not sure.

Can you try send request from RESTDebugger to Exe compiled by me.

and see in Right memo, what result will show.

Send request by this way direct in Browser: http://yourserver:port/JSON=EncodedBase64 (JSONdata).

all must be in URL.

Try to encode data with this: https://www.base64encode.org/

 

I think you are using an old unigui version, so I cannot view it with your exe.

I don't want to write a URL this is a solution that won't be right for the other party


RestFull logic is fixed all over the world.

image.png.b8a9e1c8759a8b0e100334190f420b9b.png

Link to comment
Share on other sites

6 minutes ago, pro_imaj said:

I think you are using an old unigui version

Yes, 1551.

 

6 minutes ago, pro_imaj said:

I don't want to write a URL this is a solution that won't be right for the other party

So You want to send JSON in body of request ?

 

Do you get any data from the client in TUniServerModule.UniGUIServerModuleHTTPCommand,  when testing with restdebuger?

 

Link to comment
Share on other sites

12 minutes ago, pro_imaj said:

I think you are using an old unigui version, so I cannot view it with your exe.

I don't want to write a URL this is a solution that won't be right for the other party


RestFull logic is fixed all over the world.

image.png.b8a9e1c8759a8b0e100334190f420b9b.png

In example I give You on TUniServerModule.UniGUIServerModuleHTTPCommand, this part

//check if type is JSON file
if (POS ('/JSON',ARequestInfo.URI) > 0)
then begin
      // DO YOUR REST STUFF
      AResponseInfo.ResponseNo := 200;
      AResponseInfo.ContentEncoding := '1251';
      ARequestInfo.Document := mmReadDocument (1,Copy (ARequestInfo.Document,Length ('/JSON=')+1,Length (ARequestInfo.Document)));
      AResponseInfo.ContentText := ARequestInfo.Document;
      AResponseInfo.WriteContent;
      Handled := True;
      AResponseInfo.CloseSession;
end;

must get json sended from restdebuger, did You get data there ?

If not try to send data in url (just for test) and check If you get data.

Link to comment
Share on other sites

Hello, Try to send answer to Client with this in procedure TUniServerModule.UniGUIServerModuleHTTPCommand :

//send answer to client

      AResponseInfo.ResponseNo := 200;
      AResponseInfo.ContentEncoding := '1251';
      ARequestInfo.Document := YOURJSON ANSWER
      AResponseInfo.ContentText := YOURJSON ANSWER
      AResponseInfo.WriteContent;
      Handled := True;
      AResponseInfo.CloseSession;

 

Try to read  into procedure TUniServerModule.UniGUIServerModuleHTTPCommand :

ARequestInfo.POSTStream

ARequestInfo.Params

ARequestInfo.RawHTTPCommand

 

maybe there will be data sended from REST debugger

Link to comment
Share on other sites

  • 4 months later...
On 3/4/2023 at 9:38 AM, pro_imaj said:

Hi,

Can I use it as Unigui Rest Full.

So like this;
I will send the following 1.json format as Post method to http://localhost:8077/user link.

2. I will send it as a response in json format, can this be done?

1. Json
{
   "username": "a",
   "Password": "123"
}

2.Json
{
   "Name": "bbbb",
   "Mission": "pppppp"
}


*I tried to do a lot of reasoning and examples but as a general problem. When I type http://localhost:8077/user I see the following error on the screen.

Invalid session or session Timeout. (Invalid URI: /user )

Is there a solution
Thanks

This is what you need:
 

procedure TUniServerModule.UniGUIServerModuleHTTPCommand(
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo;
  var Handled: Boolean);
var
   aStream : TStream;
   aJSONData : string;
begin
   if UpperCase(ARequestInfo.URI).Contains('/USER')  then
   begin
      if ARequestInfo.Command = 'POST' then
      begin
        aStream := ARequestInfo.PostStream;
        if assigned(aStream) then
        begin
           aStream.Position := 0;
           aJSONData := ReadStringFromStream(aStream);
           ProcessTheData(aJSONData);
           AResponseInfo.ResponseNo  := 200;
           AResponseInfo.ContentType := 'application/json';
           Handled := True;
       end
       else
       begin
          AResponseInfo.ResponseNo  := 201;
          AResponseInfo.ResponseText := 'Invalid Data.';
       end;
    end;
    if ARequestInfo.Command = 'GET' then
    begin
        aJSONData := '{"Name":"Robert Smith", "Mission":"Conquer the World"}';
        AResponseInfo.FreeContentStream := True;
        AResponseInfo.ContentStream := TStringStream.Create(aJSONData);
        AResponseInfo.ContentLength := AResponseInfo.ContentStream.Size;
        AResponseInfo.ResponseNo  := 200;
        AResponseInfo.ContentType := 'application/json';
        Handled := True;
    end;
  end;
end;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...