The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,757 other followers

Indy: getting response despite exceptions or 4xx HTTP status codes

Posted by jpluimers on 2016/12/07

Formy snippet archive (thanks Walter Prins for answering and Oliver Funcke for asking and elaborating on the answer):

in the case of error, you can get what would’ve normally been in the contentstream from the ExceptionObj.ErrorMessage property.  So you can use something like the following if you want to get the content response regardless of http response code (untested):

var 
  FResponseStream: TStringStream;
  FRequestURL, Content : String;
begin
  //.... etc etc
  try
    FIdHTTP.Get(FRequestURL, FResponseStream);
    Content := FResponseStream.DataString;
  except
    on E:EIdHTTPProtocolException do
      Content := E.ErrorMessage; 
  end;
  // At this point, "Content" contains the response body, both for 
  // successful (200) as well as other response codes.
  
  //.... etc etc
end;

….

You can even do it simpler:

Response := IdHTTP.Get('http://host/path', [404]);

Source: delphi – Indy and REST – Can I prevent exceptions? – Stack Overflow

–jeroen

via:

2 Responses to “Indy: getting response despite exceptions or 4xx HTTP status codes”

  1. Note that the AIgnoreReplies parameter is only available in TIdHTTP.Get() (and TIdHTTP.DoRequest()). You can’t use it for POST requests (unless you call DoRequest() directly). However, there is another option. The TIdHTTP.HTTPOptions property has hoNoProtocolErrorException and hoWantProtocolErrorContent flags available. If hoNoProtocolErrorException is enabled, an HTTP error will not raise EIdHTTPProtocolException, the request will just end normally. In that scenario, if hoWantProtocolErrorContent is enabled then the error content will be saved in the caller’s ContentStream, otherwise it will be discarded.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: