When using TIdHTTP to send GET commands with JSON variables, sometimes the server returns me the error below:
HTTP/1.1 500 Internal Server Error
Using Insomnia for testing JSON results, I see an entire JSON response and a pretty response which clearly indicates what the problem is. I really need that the TStringStream be filled even if the response code is not 200.
After a lot of searching, I understand that I should use HTTPOptions with the parameters: hoNoProtocolErrorException and hoWantProtocolErrorContent. However, when using the hoWantProtocolErrorContent option, I obtain the following error:
Undeclared identifier: 'hoWantProtocolErrorContent'
You are using an old version of Indy (is it the one that shipped with Delphi 7?). Delphi 7 was released in 2002, while the
hoWantProtocolErrorContentflag was introduced in 2016. Per New TIdHTTP flags and OnChunkReceived event in Indy's blog):So, without the
hoWantProtocolErrorContentflag, the only way you can access the body data of an HTTP error response is to disable thehoNoProtocolErrorExceptionflag (it is disabled by default) and then catch the raisedEIdHTTPProtocolExceptionexception and read itsErrorMessageproperty, eg:Or simpler: