Jump to content
Amos

Delphi 10.2 TNetHTTPClient vs TRestClient

Recommended Posts

2 related questions:

 

The first one is:

I found detailed explanations about TNetHTTPClient and TRestClient (and its gang) but I couldn't find an explanation about the advantage of using 3 REST components instead of one TNetHTTPClient component. I do query a rest service and receive data but I do it using TNetHTTPClient and it seems to work ok (unless I'm missing something) so I was wondering what are the benefits/advantages of using the Rest components instead.

 

The other question is:

I set the ConnectionTimeout to 120000 (2 minutes) and the ResponseTimeout to 60000000 (1000 minutes). The server is a servlet written in Java and the session timeout there is 30 minutes.

Despite the above, after 30 seconds or so, the connection is terminated and the response is nil as if something broke it in the middle of the work. How can I know if it's a client side or a server side? Am I missing something with the responsetimeout I set?

 

Thanks

 

  • Like 1

Share this post


Link to post

I have stopped using Firemonkey in July 2018 (in favor of Flutter which is awesome 😉 ) so what I am going to say could be useless (bugs have been fixed, MAYBE) but these are the issues:

 

  • There is not a clear way to handle errors with REST components (REST client / request / response) because sometimes they "randomly" raise exceptions
  • When you try to run a non-blocking operation on the background there are so many issues that I cannot write an full list. I had asked a question some time ago for example (https://stackoverflow.com/questions/50437782/delphi-rest-async-request-detect-connection-failure) but no answers. Major problems are: cannot catch exceptions in async requests, connection timeout seems to be broken because it does not actually time out and when you set params for POST requests I cannot remember which problem I had (they were not properly passed due to encoding problems; it was not a problem of mine because with C# I was able to do everything flawlessy)
  • If you try to use the 3 components with live bindings and data sets with the examples given online (or directly downloading the source code) you get N errors of M type
  • I was able to do some stuff with TOAuth1Authenticator using workarounds because it did not work under certain circumstances

 

My solution was the following: TIdHTTP. Indy is very reliable and it allowed me to create my REST service without any problem. I used to handle GET and POST requests with TIdHTTP inside a TTask.Run(); and then with some anonymous function I used to handle the async response. I had spent 3 hours on this which is nothing compared to the 2 days I had spent with REST components (trying to figure out what was wrong).

 

This is my experience and, as I have already said, bugs may have been fixed. I am telling you the problems that I found so you can save your time or see if the components now work properly. If no, you can safely remove them from Delphi and a simple solution is TIdHTTP in a worker thread. Again, if it can be useful to you, I used to write by myself the REST connection handler with TIdHTTP and some good design patterns that helped me were the following: Template Method, Singleton and Decorator. There are many more that can be useful to you, check on Google!

Edited by Alberto Miola
  • Like 1

Share this post


Link to post

Thank you very much for sharing your experience, I understand you had a very bad experience with the TRestClient (and the gang) but I wonder if you tried TNetHTTPClient and what was your experience with that.

 

I ask because out of all the things you wrote, the timeout issue is the only thing I'm dealing with now and have no clue what's going on. I do know that when the servlet is running locally, it's working flawlessly and when running remotely, I get disconnections, not sure if it's timeout related or not.

 

I will give indy a try though I preferred to use built in components and not 3rd party ones. I understand that the component that came with 10.2 is not the latest one, how can I be sure that I'm using the latest TidHTTP component in 10.2? Also, will I need the external ssl dll files for it to work with https?

Edited by Amos

Share this post


Link to post

If you are using the latest version of the IDE you should also have the latest version (or one of the most recent). If you are going to handle HTTPS, check here! I have never tried TNetHTTPClient so I can't tell nothing about it

Share this post


Link to post

I always used Indy and ICS. Both are great, but both requires extra DLL (OpenSSL) if you need to use https. Each version might required a different OpenSSL DLL version, so mixing both (indy and ICS) will lead to a bucket of inconsistent smelly stuff.
In my latest project I was asked to implement a REST client that must access HTTPS URL. I used TNetHTTPClient very successfully. It is very fast and works https without extra DLL.
All I had to do was to assign the proper header and voilá! POST, GET, PUT et al with https support. A single component to the rescue.! I tried the TRestClient component family but they seemed to be over complicated ( I just ruled them out )

  • Like 1

Share this post


Link to post

My experience exactly so I guess the TRest family can be avoided.

 

One last thing is to understand the disconnections I have, not sure if it's client or server side. Any tips on finding that out?

Share this post


Link to post
On 5/22/2019 at 3:45 PM, Alberto Miola said:

I have stopped using Firemonkey in July 2018 (in favor of Flutter which is awesome 😉 ) so what I am going to say could be useless (bugs have been fixed, MAYBE) but these are the issues:

 

  • There is not a clear way to handle errors with REST components (REST client / request / response) because sometimes they "randomly" raise exceptions
  • When you try to run a non-blocking operation on the background there are so many issues that I cannot write an full list. I had asked a question some time ago for example (https://stackoverflow.com/questions/50437782/delphi-rest-async-request-detect-connection-failure) but no answers. Major problems are: cannot catch exceptions in async requests, connection timeout seems to be broken because it does not actually time out and when you set params for POST requests I cannot remember which problem I had (they were not properly passed due to encoding problems; it was not a problem of mine because with C# I was able to do everything flawlessy)
  • If you try to use the 3 components with live bindings and data sets with the examples given online (or directly downloading the source code) you get N errors of M type
  • I was able to do some stuff with TOAuth1Authenticator using workarounds because it did not work under certain circumstances

 

My solution was the following: TIdHTTP. Indy is very reliable and it allowed me to create my REST service without any problem. I used to handle GET and POST requests with TIdHTTP inside a TTask.Run(); and then with some anonymous function I used to handle the async response. I had spent 3 hours on this which is nothing compared to the 2 days I had spent with REST components (trying to figure out what was wrong).

 

This is my experience and, as I have already said, bugs may have been fixed. I am telling you the problems that I found so you can save your time or see if the components now work properly. If no, you can safely remove them from Delphi and a simple solution is TIdHTTP in a worker thread. Again, if it can be useful to you, I used to write by myself the REST connection handler with TIdHTTP and some good design patterns that helped me were the following: Template Method, Singleton and Decorator. There are many more that can be useful to you, check on Google!

I too was seeing crashes in my FMX app running on mac when exceptions would occur using the REST components.  This was back with XE8 and I ended up just building the app in Swift.

  • Like 1

Share this post


Link to post
Guest

Interesting... so far in FMX I have always used REST components for iOS and Android (RAD Tokyo) and iOS (RAD Rio) with no problems, completely async. from the start. Just curious, what issues you had?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×