Recent

Author Topic: indy. how download file from github api  (Read 1004 times)

RDL

  • New Member
  • *
  • Posts: 38
indy. how download file from github api
« on: September 24, 2021, 09:40:41 am »
Hi.
There is a line curl
Code: Pascal  [Select][+][-]
  1. curl -O -L -H "Accept: application/octet-stream" -H "Authorization: token mytoken" https://api.github.com/repos/myowner/myrepo/releases/assets/123
Uploading the file from github is successful.

I am trying to do the same via idhttp

Code: Pascal  [Select][+][-]
  1. SSL:=TIdSSLIOHandlerSocketOpenSSL.Create;
  2. SSL.SSLOptions.SSLVersions:=[sslvSSLv2,sslvSSLv23,sslvSSLv3,sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];  
  3. HTTP:=TIdHTTP.Create;
  4. HTTP.IOHandler:=SSL;
  5. HTTP.HandleRedirects:=True;
  6. HTTP.ConnectTimeout:=5000;
  7. HTTP.ReadTimeout:=5000;
  8. HTTP.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
  9. HTTP.Request.CustomHeaders.AddValue('Accept','application/octet-stream');
  10. HTTP.Request.CustomHeaders.AddValue('Authorization','token mytoken');
  11. MS:=TMemoryStream.Create;
  12. HTTP.Get('https://api.github.com/repos/myowner/myrepo/releases/assets/123',MS);
  13. MS.SaveToFile(SaveFile);
  14. MS.Free;
  15. HTTP.Free;
  16.  

But the file is not loaded, the response from the github api is saved instead.
What am I doing wrong?

PS. I can write a real download link from github if needed
« Last Edit: September 24, 2021, 09:43:58 am by RDL »
Sorry for my english, google translation!

RDL

  • New Member
  • *
  • Posts: 38
Re: indy. how download file from github api
« Reply #1 on: September 24, 2021, 03:01:21 pm »
Adding
Code: Pascal  [Select][+][-]
  1. HTTP.Request.Accept: = EmptyStr;
solved my problem.
Thanks.
Sorry for my english, google translation!

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1021
    • Lebeau Software
Re: indy. how download file from github api
« Reply #2 on: September 24, 2021, 09:08:57 pm »
Code: Pascal  [Select][+][-]
  1. HTTP.Request.CustomHeaders.AddValue('Accept','application/octet-stream');
Adding
Code: Pascal  [Select][+][-]
  1. HTTP.Request.Accept: = EmptyStr;
solved my problem.

Why are you using the Request.CustomHeaders property to set the Accept header at all?  You should be setting the Request.Accept property to the desired value, eg:

Code: Pascal  [Select][+][-]
  1. HTTP.Request.Accept: = 'application/octet-stream';

In any case, you are leaking your SSL object, and you will leak your MS and HTTP objects as well if TIdHTTP.Get() raises an exception.

I would use this code instead:

Code: Pascal  [Select][+][-]
  1. HTTP := TIdHTTP.Create;
  2. try
  3.   SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
  4.   SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];  
  5.   HTTP.IOHandler := SSL;
  6.   HTTP.HandleRedirects := True;
  7.   HTTP.ConnectTimeout := 5000;
  8.   HTTP.ReadTimeout := 5000;
  9.   HTTP.ProtocolVersion := pv1_0;
  10.   HTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
  11.   HTTP.Request.Accept := 'application/octet-stream';
  12.   HTTP.Request.BasicAuthentication := False;
  13.   HTTP.Request.CustomHeaders.AddValue('Authorization', 'token mytoken');
  14.   MS := TMemoryStream.Create;
  15.   try
  16.     HTTP.Get('https://api.github.com/repos/myowner/myrepo/releases/assets/123', MS);
  17.     MS.SaveToFile(SaveFile);
  18.   finally
  19.     MS.Free;
  20.   end;
  21. finally
  22.   HTTP.Free;
  23. end;
« Last Edit: September 24, 2021, 09:12:50 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

RDL

  • New Member
  • *
  • Posts: 38
Re: indy. how download file from github api
« Reply #3 on: September 25, 2021, 07:51:16 am »
Remy Lebeau, thanks for the answer.
In my working code, "try/except/finally" is accounted for.
Here I wrote it just as an example.
Thanks again! :)
Sorry for my english, google translation!

 

TinyPortal © 2005-2018