Jump to content
giomach

Using IdHTTP

Recommended Posts

I'm likely to have further elementary questions about IdHTTP, so if someone can recommend a reference on using it to access REST in the context of Delphi (and preferably a written reference rather than a video), that might save time and bother.

 

Meanwhile, here's my first simple question.

 

The Delphi statement

                            s := IdHTTP1.Get (url+filename);

where s is a string successfully fetches data from a remote server.

But when the remote file is actually a sound file, having it in a string is not much use to me.

How can I get it into a sound file on my local machine?

 

Thanks in advance for your advice.
 

 

Share this post


Link to post

Try something like this:

 

var
  s: TStream;
begin
  s := TFileStream.Create('soundfile.mp3', fmCreate);
  try
    IdHttp1.Get(your_file_url, s);
  finally
    s.Free;
  end;
end;

 

  • Like 1

Share this post


Link to post
Posted (edited)

Thanks Ioan, that worked great.

 

My next question to all 🙂  Many of the files I'll be looking for will not exist on the server.  Is there a way to test for the existence of a remote file without actually trying to download it, and importantly would that be quicker than what I'm doing, which is trying to download and checking whether the size is non-zero?  (I will treat a non-existing remote file and an existing file of zero size in the same way, so I don't need to distinguish those two cases.)

Edited by giomach
spelling correction

Share this post


Link to post
4 hours ago, giomach said:

Is there a way to test for the existence of a remote file without actually trying to download it

Yes - use TIdHTTP.Head() instead of TIdHTTP.Get().  A HEAD request is identical to a GET request except that no body data is sent, but the response headers will be the same.

4 hours ago, giomach said:

and importantly would that be quicker than what I'm doing, which is trying to download and checking whether the size is non-zero?  (I will treat a non-existing remote file and an existing file of zero size in the same way, so I don't need to distinguish those two cases.)

A HEAD response will include the 'Content-Length' header, so yes, you can determine the file's size without actually downloading the file.

 

However, if you are going to download the file anyway then there is really no point in checking for its existence beforehand, just download the file and handle whatever error may arise, such as HTTP 404 when the file does not exist.

  • Like 1

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
×