Forum > Networking and Web Programming
[SOLVED] FPHTTPClient + GitHub = :)
Trenatos:
I'm working on a CLI tool, it's working nicely but I've run into a strange issue.
I'm using FPHTTPClient to download various files, works fine, except with GitHub.
This works: WriteLn( TFPHTTPClient.SimpleGet('https://httpbin.org/uuid') );
This doesn't: WriteLn( TFPHTTPClient.SimpleGet('https://api.github.com/repos/OpenBD/openbd-core/releases/latest') );
I have the OpenSSL DLLs and that part seems fine, but when I try hitting the GitHub API nothing seems to work.
ResponseStatusCode returns 0, and data just spits out a blank line.
I'm lost.. Anyone seen this behaviour before?
--- 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";}};} --- FHTTPClient := TFPHTTPClient.Create(nil); try FHTTPClient.AllowRedirect:=true; data := FHTTPClient.Get( 'http://api.github.com/repos/OpenBD/openbd-core/releases/latest' ); WriteLn(FHTTPClient.ResponseStatusCode); WriteLn( data ); finally FHTTPClient.Free; end;
The url works fine in my browser, it works fine in Insomnia (Desktop REST utility)
Cyrax:
Maybe setting user agent variable helps?
balazsszekely:
Try this:
--- 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";}};} ---function GetJSON(const AURL: String; out AJSON: TJSONStringType): Boolean;var Ms: TMemoryStream; HTTPClient: TFPHTTPClient;begin Result := False; Ms := TMemoryStream.Create; try HTTPClient := TFPHTTPClient.Create(nil); try HTTPClient.AllowRedirect := True; HTTPClient.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); HTTPClient.AddHeader('Content-Type', 'application/json'); HTTPClient.HTTPMethod('GET', AURL, MS, []); if HTTPClient.ResponseStatusCode = 200 then begin if Ms.Size > 0 then begin MS.Position := 0; SetLength(AJSON, MS.Size); MS.Read(Pointer(AJSON)^, Length(AJSON)); Result := Length(AJSON) > 0; end; end; except Result := False; end; finally Ms.Free; end;end; var JSON: TJSonStringType;begin if GetJSON('https://api.github.com/repos/OpenBD/openbd-core/releases/latest', JSON) then Writeln(JSON);end.
molly:
--- Quote from: Cyrax on April 11, 2018, 05:59:43 am ---Maybe setting user agent variable helps?
--- End quote ---
Not maybe. Github API demands it.
Trenatos:
AHA! I hadn't seen that molly, thanks!
I haven't had a chance to try out your solution yet GetMem but hopefully shortly.
Navigation
[0] Message Index
[#] Next page