When Indy get a 404, I cannot get the response content, its always empty.
For example, using Indy to access https://www.etsy.com/people/car, I get a 404 and contentstream (or assigned stream) is just empty. Doing the same with THttpClient, I also get a 404, but also get the content of the 404 page in the response stream.
So my question is, how can I get the response content with Indy when its a 404?
using Indy to access https://www.etsy.com/people/car, I get a 404 and contentstream (or assigned stream) is just empty.
This is the default behavior, by design, to prevent corrupting the output with unexpected error data that was not requested.
how can I get the response content with Indy when its a 404?
There are two possible ways:
By default, TIdHTTP
raises an EIdHTTPProtocolException
exception on an HTTP error. The response data will be in its ErrorMessage
property.
You can avoid the raised exception by either:
enabling the hoNoProtocolErrorException
flag in the TIdHTTP.HTTPOptions
property.
using the overloaded version of TIdHTTP.Get()
that has an AIgnoreReplies
parameter, passing 404 (or any other error response code you want) in that parameter.
Either way, by default the response data will still be discarded. You can avoid that by enabling the recently added hoWantProtocolErrorContent
flag in the TIdHTTP.HTTPOptions
property (see New TIdHTTP flags and OnChunkReceived event).