Forum > Networking and Web Programming

indy. how download file from github api

(1/1)

RDL:
Hi.
There is a line curl

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---curl -O -L -H "Accept: application/octet-stream" -H "Authorization: token mytoken" https://api.github.com/repos/myowner/myrepo/releases/assets/123Uploading the file from github is successful.

I am trying to do the same via idhttp


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SSL:=TIdSSLIOHandlerSocketOpenSSL.Create;SSL.SSLOptions.SSLVersions:=[sslvSSLv2,sslvSSLv23,sslvSSLv3,sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];  HTTP:=TIdHTTP.Create;HTTP.IOHandler:=SSL;HTTP.HandleRedirects:=True;HTTP.ConnectTimeout:=5000;HTTP.ReadTimeout:=5000;HTTP.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';HTTP.Request.CustomHeaders.AddValue('Accept','application/octet-stream');HTTP.Request.CustomHeaders.AddValue('Authorization','token mytoken');MS:=TMemoryStream.Create;HTTP.Get('https://api.github.com/repos/myowner/myrepo/releases/assets/123',MS);MS.SaveToFile(SaveFile);MS.Free;HTTP.Free; 
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

RDL:
Adding

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTP.Request.Accept: = EmptyStr;solved my problem.
Thanks.

Remy Lebeau:

--- Quote from: RDL on September 24, 2021, 09:40:41 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTP.Request.CustomHeaders.AddValue('Accept','application/octet-stream');
--- End quote ---

--- Quote from: RDL on September 24, 2021, 03:01:21 pm ---Adding

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTP.Request.Accept: = EmptyStr;solved my problem.

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---HTTP := TIdHTTP.Create;try  SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);  SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];    HTTP.IOHandler := SSL;  HTTP.HandleRedirects := True;  HTTP.ConnectTimeout := 5000;  HTTP.ReadTimeout := 5000;  HTTP.ProtocolVersion := pv1_0;  HTTP.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';  HTTP.Request.Accept := 'application/octet-stream';  HTTP.Request.BasicAuthentication := False;  HTTP.Request.CustomHeaders.AddValue('Authorization', 'token mytoken');  MS := TMemoryStream.Create;  try    HTTP.Get('https://api.github.com/repos/myowner/myrepo/releases/assets/123', MS);    MS.SaveToFile(SaveFile);  finally    MS.Free;  end;finally  HTTP.Free;end;

RDL:
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! :)

Navigation

[0] Message Index

Go to full version