1

I am trying to get around the Indy idHTTP Component and have some issues retrieving the server response after a GET/PUT/POST if it's not 200 OK. Even if I disable the ProtocolException, Response.ResponseText AND the reutrned StringStream only show the 400 Error but not the content the server transmitted.

How can I get that data???

thx in advance

Wolfgang Bures
  • 509
  • 4
  • 12

1 Answers1

1

To avoid discarding response body by TIdHTTP component in case of non-2xx status, you'd also need to use hoWantProtocolErrorContent among HttpOptions. Unfortunatelly these options are poorly documented even in help file shipped with RAD Studio help (HelpThird-Party HelpIndy Library Help).

To get the response body as a string use Get/Put/Post invariant that returns string, not TIdHTTP.Response.ResponseText or TIdHTTP.ResponseText for short which contains status line of HTTP response; e.g.: HTTP/1.1 400 Bad Request.

Sample code:

var
  IdHTTP: TIdHTTP;
begin
  IdHTTP := TIdHTTP.Create();
  try
    IdHTTP.HTTPOptions := IdHTTP.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];
    Writeln(IdHTTP.ResponseText);
    Writeln;
    Writeln(IdHTTP.Get('https://postman-echo.com/status/400'));
  finally
    IdHTTP.Free;
  end;
end;

Sample output:

HTTP/1.1 400 Bad Request

{"status":400}

Peter Wolf
  • 3,700
  • 1
  • 15
  • 30
  • "*Unfortunatelly these options are poorly documented even in help file shipped with RAD Studio help*" - that is because Indy's documentation hasn't been updated in a LONG LONG time. A lot of new functionality added in Indy 10.6.x hasn't been documented yet, including `hoWantProtocolErrorContent`. That flag is described in [this blog post](https://www.indyproject.org/2016/01/10/new-tidhttp-flags-and-onchunkreceived-event/) on Indy's website, though – Remy Lebeau Jan 03 '20 at 17:29