The Wiert Corner – irregular stream of stuff

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

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

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

    Join 2,969 other subscribers

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):

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


You can even do it simpler:

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

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



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: Logo

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: