Atozed Forums
JSON in Get request - Printable Version

+- Atozed Forums (https://www.atozed.com/forums)
+-- Forum: Indy (https://www.atozed.com/forums/forum-8.html)
+--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html)
+--- Thread: JSON in Get request (/thread-2774.html)



JSON in Get request - CarlAarnes - 06-21-2022

Hi,

How can i add some json into the http request body when using GET?


RE: JSON in Get request - rlebeau - 06-21-2022

(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;



RE: JSON in Get request - CarlAarnes - 06-22-2022

Thank you a lot, thisĀ seems to do what I wanted.