delphiindyidhttp

Delphi /w Indy 10: Get Content after ERROR 400 (idHTTP)


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


Solution

  • 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 (Helpā†’Third-Party Helpā†’Indy 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}