Rain
1
Hello everyone!
I am trying to get video information using Twitch API, but i confused with results… It’s so strange…
Dev API page says i must send this GET request:
https://api.twitch.tv/kraken/videos/<video ID>
Okay… If i using CURL in windows console with command:
curl -H "Accept: application/vnd.twitchtv.v5+json" -H "Client-ID: <ID>" -X GET "https://api.twitch.tv/kraken/videos/370746236"
It’s working fine. I get correct JSON-file.
But if i write my code in Delphi:
var
t, cmd : string;
begin
IdHTTP1.Request.CustomHeaders.Clear;
IdHTTP1.Request.CustomHeaders.Add('Accept: application/vnd.twitchtv.v5+json');
IdHTTP1.Request.CustomHeaders.Add('Client-ID: <ID>');
cmd := 'https://api.twitch.tv/kraken/videos/370746236';
t := IdHTTP1.Get(cmd);
MemoJSON.Lines.Text := t;
end;
It works, but recieves wrong JSON-file which missing some important info. E.g. all URLs to big thumbnails previews is missed, and some other fields.
What’s wrong? How to fix it?
Please help!
The output seens fine to me
If it’s a BRAND NEW VOD, the thumbnails might not exist yet. But otherwise no issue can be seen here
Rain
3
How did you send this request?
I need to send it from Delphi, but it returning an incomplete JSON-file
Just a standard HTTP request in a browser which is essentially identical to performing it via cURL or other HTTP Library.
curl -H 'Accept: application/vnd.twitchtv.v5+json' \
-H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-X GET 'https://api.twitch.tv/kraken/videos/370746236'
(Just with a real client ID)
Might be an issue with your JSON decoder, but there’s nothing special in the thumbnail section that could throw a wobble.
Rain
5
Then why does the Delphi program recieves the wrong file from the server? How to fix it?
Here is the file i get: https://pastebin.com/mNxkQEji. It’s incomplete. Why?
I not decoding JSON-file. I just need to receive it correctly. Decoding it is not a problem.
What you mean saying it? I need to get these picture files.
It’s not incomplete. That’s the v3 output for that endpoint
You did not specify the v5 header and thus you are getting the v3 response.
You need to add
Accept: application/vnd.twitchtv.v5+json
as a header when you make the request
Rain
7
but then i do
IdHTTP1.Request.CustomHeaders.Add('Accept: application/vnd.twitchtv.v5+json');
Is something wrong?
I’m trying to google and read the Delphi docs for you
Maybe
IdHttp1.Request.CustomHeaders.AddValue('key', 'value');
?
Sounds like your second call to the header adding function is wiping out the first header
Rain
9
It’s impossible! I’m trying to do it in my just created clean test project: https://pastebin.com/2Av9HA3s.
Any ideas?
For whatever reason your Delphi HTTP calls are not sending both headers. I can’t find anything in google to help with this. I’m at a loss with it right now.
Rain
11
Yes!!! I did it! 
there is my code:
function Https_Get(ServerName, Resource : string;
var res : string;
var ErrorCode : Integer) : Boolean;
const
sUserAgent = 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101';
BufferSize = 1024*64;
var
hInet : HINTERNET;
hConnect : HINTERNET;
hRequest : HINTERNET;
lpdwBufferLength: DWORD;
lpdwReserved : DWORD;
dwBytesRead : DWORD;
lpdwNumberOfBytesAvailable : DWORD;
b, _pos : Cardinal;
ResponseText : AnsiString;
Header : string;
p : PChar;
begin
Result := False;
hInet := InternetOpen(PChar(sUserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
if Assigned(hInet) then
begin
hConnect := InternetConnect(hInet, PChar(ServerName),
INTERNET_DEFAULT_HTTPS_PORT, nil, nil,
INTERNET_SERVICE_HTTP, 0, 0);
if Assigned(hConnect) then
begin
p := PChar('Accept: application/vnd.twitchtv.v5+json'+ #0);
hRequest := HttpOpenRequest(hConnect, 'GET', PChar(Resource),
HTTP_VERSION, '', @p, INTERNET_FLAG_SECURE, 0);
if Assigned(hRequest) then
begin
Header := 'Client-ID: <ID>';
if not HttpSendRequest(hRequest, PChar(Header), Length(Header), nil, 0) then
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInet);
Exit;
end;
lpdwBufferLength := SizeOf(dword);
lpdwReserved := 0;
if not HttpQueryInfo(hRequest,
HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
@ErrorCode, lpdwBufferLength, lpdwReserved) then
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInet);
Exit;
end;
if ErrorCode = 200 then
begin
_Pos := 1;
b := 1;
ResponseText := '';
while b > 0 do
begin
if not InternetQueryDataAvailable( hRequest,
lpdwNumberOfBytesAvailable, 0, 0 ) then
begin
Result := False;
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInet);
Exit;
end;
SetLength( ResponseText, Length(ResponseText) + lpdwNumberOfBytesAvailable );
InternetReadFile(hRequest, @responsetext[_pos],
lpdwNumberOfBytesAvailable, b );
Inc(_Pos, b);
end;
res := UTF8ToString(ResponseText);
Result := True;
end;
InternetCloseHandle(hRequest);
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hInet);
end;
end;
//using:
var
JsonString : string;
ErrorCode : integer;
begin
if https_get('api.twitch.tv', '/kraken/channels/<channel ID>/videos?limit=100&broadcast_type=all', JsonString, ErrorCode) then
memo1.Lines.Text := JsonString;
end;
It’s working! Thank you!
1 Like
system
Closed
12
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.