06-21-2022, 04:05 PM
06-21-2022, 04:31 PM
(06-21-2022, 04:05 PM)CarlAarnes Wrote: [ -> ]How can i add some json into the http request body when using GET?
TIdHTTP.Get() does not currently allow you to do this. While the current HTTP specs do not strictly prohibit sending a body with a GET request, they do leave it very ambiguous how servers should handle such requests, leading to a wide range of inconsistent/non-functional behaviors.
That being said, if you absolutely need to include a body in a GET request (because a particular REST API requires it), you will have to call the TIdCustomHTTP.DoRequest() method directly, eg:
Code:
// need an accessor class because DoRequest() is protected...
type
TIdHTTPAccess = class(TIdHTTP)
end;
var
requestData: TStringStream;
responseData: TMemoryStream;
begin
responseData := TMemoryStream.Create;
try
requestData := TStringStream.Create(JSON, TEncoding.UTF8);
try
TIdHTTPAccess(IdHTTP1).DoRequest('GET', url, requestData, responseData, []);
finally
requestData.Free;
end;
// process responseData as needed...
finally
responseData.Free;
end;
end;
06-22-2022, 02:27 PM
Thank you a lot, thisĀ seems to do what I wanted.