Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON in Get request
#1
Hi,

How can i add some json into the http request body when using GET?
Reply
#2
(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;

Reply
#3
Thank you a lot, this seems to do what I wanted.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)