soon Posted September 24, 2016 Share Posted September 24, 2016 Hi all, im new to UNIGUI Can anyone provide a sample of how does Unigui client call API/web services with parameters and read the returned JSON data list and display in grid ? thank you Quote Link to comment Share on other sites More sharing options...
Ron Posted September 24, 2016 Share Posted September 24, 2016 I don't have an example right now, but a web service is basically just an HTTP server, so use any HTTP client and make a call, and read the result, XML or JSON....parse it, push it into a table and refresh the query linked to your grid, and there you have it. I like using the Indy components, the HTTP client is blocking, so use a thread if you have to, but it is very easy to set up and there are tons of examples on the net. Quote Link to comment Share on other sites More sharing options...
soon Posted September 26, 2016 Author Share Posted September 26, 2016 I don't have an example right now, but a web service is basically just an HTTP server, so use any HTTP client and make a call, and read the result, XML or JSON....parse it, push it into a table and refresh the query linked to your grid, and there you have it. I like using the Indy components, the HTTP client is blocking, so use a thread if you have to, but it is very easy to set up and there are tons of examples on the net. Do you mean TIdHTTP ? Quote Link to comment Share on other sites More sharing options...
Ron Posted September 26, 2016 Share Posted September 26, 2016 @soon: yes, the indy http client. Below is a function I previously used to send SMS via a phone operator web service, using http.get and no ssl: function TuniMainModule.SendSMS(number, msg:string):string; const cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)'; var httpResponse: string; Stream: TStringStream; URL, telParams:string; HTTP:TidHTTP; begin URL:='http://admin.telefonkatalogen.no/smsgateway/sendSms?sender='+shortNumber+'&targetNumbers='+number+'&sms='+encodeMessage(msg)+'&userName='+Username+'&password='+tSmsPw; Stream := TStringStream.Create; HTTP:=TIdHTTP.Create(nil); HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; } HTTP.ConnectTimeout := 10000; try HTTP.Request.UserAgent := cUSER_AGENT; try HTTP.Get(URL, Stream); result:= Stream.DataString; except result:= 'SMS Error!'; end; finally Stream.Free; HTTP.Free; end; end; If you want basic http auth then add: HTTP.Request.BasicAuthentication:=true; HTTP.Request.Username:=username; HTTP.Request.Password:=pw; If you need SSL, add: var LHandler: TIdSSLIOHandlerSocketOpenSSL; ... try LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); ... HTTP.IOHandler:=LHandler; finally LHandler.Free; etc. If you need to POST, use HTTP.Post(url, aParams, aResponse); Notice: using GET you have to think about encoding, since this is part of the URL, but when using POST there is no worry about that. Quote Link to comment Share on other sites More sharing options...
soon Posted September 27, 2016 Author Share Posted September 27, 2016 @soon: yes, the indy http client. Below is a function I previously used to send SMS via a phone operator web service, using http.get and no ssl: function TuniMainModule.SendSMS(number, msg:string):string; const cUSER_AGENT = 'Mozilla/4.0 (MSIE 6.0; Windows NT 5.1)'; var httpResponse: string; Stream: TStringStream; URL, telParams:string; HTTP:TidHTTP; begin URL:='http://admin.telefonkatalogen.no/smsgateway/sendSms?sender='+shortNumber+'&targetNumbers='+number+'&sms='+encodeMessage(msg)+'&userName='+Username+'&password='+tSmsPw; Stream := TStringStream.Create; HTTP:=TIdHTTP.Create(nil); HTTP.ReadTimeout := 10000;{ IdTimeoutInfinite; } HTTP.ConnectTimeout := 10000; try HTTP.Request.UserAgent := cUSER_AGENT; try HTTP.Get(URL, Stream); result:= Stream.DataString; except result:= 'SMS Error!'; end; finally Stream.Free; HTTP.Free; end; end; If you want basic http auth then add: HTTP.Request.BasicAuthentication:=true; HTTP.Request.Username:=username; HTTP.Request.Password:=pw; If you need SSL, add: var LHandler: TIdSSLIOHandlerSocketOpenSSL; ... try LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); ... HTTP.IOHandler:=LHandler; finally LHandler.Free; etc. If you need to POST, use HTTP.Post(url, aParams, aResponse); Notice: using GET you have to think about encoding, since this is part of the URL, but when using POST there is no worry about that. ok, thank you so much for your help 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.