Basically a function checks the validity of the credentials and returns this string to me
You are not sending any credentials, at least not with any HTTP authentication anyway. Are they in your URL string instead? That is not very secure, unless HTTPS is being used.
Also, why are you using the overloaded version of TIdHTTP.Get() that fills a TStream, rather than using the version of TIdHTTP.Get() that returns a String?
function TNGITController.GetHTML(url: string): string;
var
idhttp1: TIdHTTP;
begin
Result := '';
idhttp1 := TIdHTTP.Create;
try
idhttp1.ReadTimeout := 5000;
Result := idhttp1.Get(url);
finally
idhttp1.Free;
end;
end;
But when I use this code to recover the data
...
I get this result
I don't understand what you are describing. You are clearly getting a success reply from the server with a JSON payload, so what is the actual problem you are having? Please be more specific.
Are you suggesting that the entire HTTP message, not just its body data, is being stored in the TMemoryStream? I guarantee that is not how TIdHTTP.get() works.
But it does not always happen, only occasionally.
WHAT doesn't always happen?
It is also fair to tell you that the token is sent via the AResponseInfo object: TIdHTTPResponseInfo
Like this: AResponseInfo.ContentText
How the server end is implemented is not relevant to this issue, unless the server is not populating the AResponseInfo correctly.
Where am I doing wrong?
We can't know, because you haven't described the problem clearly enough.