Jump to content
alank2

Can I use the TidHTTP component for this?

Recommended Posts

I only have one application that needs to communicate with a web API.  I've been using the ICS TSslHttpCli/TSslContext components to do this, but I wonder if I could have one less library dependency and use the built in TIdHTTP instead.  This is essentially my communication where I am sending some JSON and then getting some JSON returned:

 

Could TIdHTTP be up for this same task?

 


  //set properties
  SslHttpCli->Timeout=300;
  SslHttpCli->SslContext=SslContext;
  SslContext->SslSessionTimeout=300;
  SslHttpCli->ContentTypePost=L"application/json";
  SslHttpCli->ServerAuth=httpAuthNtlm;

 

  SslHttpCli->SendStream=parms->senddata;
  SslHttpCli->RcvdStream=parms->recvdata;
  SslHttpCli->URL=parms->URL;
  SslHttpCli->Username=parms->Username;
  SslHttpCli->Password=parms->Password;


  try
    {
      SslHttpCli->Post();
    }
  catch (Exception &exception)
    {
      ExceptionErrorMessage(&exception, NULL, parms->exmsg, 1024);
      delete SslHttpCli;
      goto fail2;
    }

 

Share this post


Link to post

If you use the newer TSslHttpRest instead of TSslHttpCli, you don't need TSslContext and can simplify your application considerably. 

 

Angus

 

 

Share this post


Link to post
Posted (edited)
3 hours ago, alank2 said:

I've been using the ICS TSslHttpCli/TSslContext components to do this, but I wonder if I could have one less library dependency and use the built in TIdHTTP instead.

You could use TIdHTTP, but you would merely be substituting one library dependency for another.  Indy is not a "built-in" library, it is a 3rd party library that just happens to be pre-installed in the IDE by default.  A true native built-in solution would be to use Embarcadero's own THTTPClient/TNetHTTPClient instead, see Using an HTTP Client and What is the difference between THTTPClient and TNetHTTPClient?.

Quote

Could TIdHTTP be up for this same task?

Technically yes, for example:

#include <IdAuthenticationNTLM.hpp>
...

//set properties
IdHTTP->ConnectTimeout = ...;
IdHTTP->ReadTimeout = ...;
IdHTTP->IOHandler = IdSSLIOHandlerSocketOpenSSL;

IdHTTP->Request->ContentType = L"application/json";
IdHTTP->Request->Username = parms->Username;
IdHTTP->Request->Password = parms->Password;

try
{
    IdHTTP->Post(parms->URL, parms->senddata, parms->recvdata);
}
catch (const Exception &exception)
{
    ExceptionErrorMessage(const_cast<Exception*>(&exception), NULL, parms->exmsg, 1024);
    delete IdHTTP;
    goto fail2;
}

 

Edited by Remy Lebeau

Share this post


Link to post

Thank you both for the help!!

 

Angus> If you use the newer TSslHttpRest instead of TSslHttpCli, you don't need TSslContext and can simplify your application considerably. 

 

Can you elaborate on the things that would be simplified?  Do you mean the way I am using it in code, or the DLL's that need to be included, etc.

 

Remy>You could use TIdHTTP, but you would merely be substituting one library dependency for another.  Indy is not a "built-in" library, it is a 3rd party library that just happens to be pre-installed in the IDE by default. 

 

I understand, but at least as I move to newer RAD studio versions, it would move too.

 

Remy>A true native built-in solution would be to use Embarcadero's own THTTPClient/TNetHTTPClient instead, see Using an HTTP Client and What is the difference between THTTPClient and TNetHTTPClient?. 

 

Interesting - I didn't know there was another option besides Indy.  Does THTTPClient also support SSL and the type of json submit/receive that I need to do?

 

 

Share this post


Link to post

I suggest you build the OverbyteIcsHttpRestTst,dpr sample and it will become obvious how it is of benefit to you.

 

Angus

 

Share this post


Link to post
Posted (edited)
2 hours ago, alank2 said:

I understand, but at least as I move to newer RAD studio versions, [Indy] would move too.

Well.... ultimately, the goal is to actually get Indy out of the IDE installation and into GetIt.  It would still be available if you needed it, but wouldn't be forcibly installed and wasting space if you didn't need it.  But, that dream is likely not to be realized anytime in the near future...

Quote

Interesting - I didn't know there was another option besides Indy.  Does THTTPClient also support SSL and the type of json submit/receive that I need to do?

Yes.  And it doesn't rely on OpenSSL DLLs to do it, either.  It uses OS-native APIs to handle that.

Edited by Remy Lebeau

Share this post


Link to post
14 hours ago, alank2 said:

Interesting - I didn't know there was another option besides Indy.  Does THTTPClient also support SSL and the type of json submit/receive that I need to do?

It does its job pretty well. I recently implemented a request routine with it and I'm pretty satisfied though some things look overcomplicated (credentials, for example)

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

×