pro_imaj Posted March 3, 2023 Share Posted March 3, 2023 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 Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 3, 2023 Author Share Posted March 3, 2023 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; Quote Link to comment Share on other sites More sharing options...
andyhill Posted March 3, 2023 Share Posted March 3, 2023 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; Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 4, 2023 Author Share Posted March 4, 2023 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. Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 4, 2023 Share Posted March 4, 2023 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; Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 6, 2023 Author Share Posted March 6, 2023 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"} Rest_Full.zip Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 6, 2023 Share Posted March 6, 2023 7 minutes ago, pro_imaj said: While sending the data, I want to send it as below with rest debugger. Hello, try with this REST server. REST Server.zip Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 6, 2023 Author Share Posted March 6, 2023 22 minutes ago, irigsoft said: Hello, try with this REST server. REST Server.zip 1.8 MB · 0 downloads 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 Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 6, 2023 Share Posted March 6, 2023 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/ Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 6, 2023 Author Share Posted March 6, 2023 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. Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 6, 2023 Share Posted March 6, 2023 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? Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 6, 2023 Share Posted March 6, 2023 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. 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. Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 6, 2023 Share Posted March 6, 2023 Try to change AResponseInfo.ContentText in TUniServerModule.UniGUIServerModuleHTTPCommand Quote Link to comment Share on other sites More sharing options...
irigsoft Posted March 7, 2023 Share Posted March 7, 2023 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 Quote Link to comment Share on other sites More sharing options...
pro_imaj Posted March 8, 2023 Author Share Posted March 8, 2023 On 3/6/2023 at 3:20 PM, irigsoft said: Evet, 1551. Yani istek gövdesinde JSON göndermek ister misiniz? @Farshad MohajeriCan't unigui be used as the world's standard RestFull server? Quote Link to comment Share on other sites More sharing options...
Norm Posted July 10, 2023 Share Posted July 10, 2023 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; Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 10, 2023 Share Posted July 10, 2023 ReadStringFromStream ? ProcessTheData(aJSONData); ? Quote Link to comment Share on other sites More sharing options...
Norm Posted July 10, 2023 Share Posted July 10, 2023 Not sure what you are querying. ReadStringFromStream is a Delphi function ProcessTheData implies some function that he will create to do whatever with the incoming data. Quote Link to comment Share on other sites More sharing options...
andyhill Posted July 10, 2023 Share Posted July 10, 2023 I was asking for the unit that holds this function (IdGlobal) Also the Process Function parameter breakdown Quote Link to comment Share on other sites More sharing options...
Norm Posted July 10, 2023 Share Posted July 10, 2023 OK. I was not sure if you were questioning the solution. Obviously ProcessTheData is a fictitious function. I will post a more detailed example of a master/details JSON transaction tomorrow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.