5

When using TIdHttp like this:

Memo1.Text := IdHTTP1.post(url,data);

I can get response content to memo1 if it doesn't give http error. But when it gives http bad request, Indy doesn't give me content. I'm also using try..except but it only prevent error box and still doesn't give me content.

How can I get content even it returns http error?

Someone
  • 728
  • 2
  • 12
  • 23
  • no I mean when it gives http bad request 400 I can see its response content in Fiddler so it definitely send content but I cannot get it in delphi. delphi just gives error. – Someone Aug 31 '13 at 14:59
  • 1
    in this comment (http://stackoverflow.com/questions/7762584/post-problems-with-indy-tidhttp#comment9481513_7769356) you say "Firefox displays the content even if he get HTTP 400 Bad Request error. E.g. Internet Explorer tell you that bad request happened and doesn't display the XML content response, Indy works the same, create the error message and throw away the content." i think my problem is exactly that. – Someone Aug 31 '13 at 15:02
  • 1
    `TIdHTTP` does not throw away the content. It is placed in the exception that is raised. See my answer for details. – Remy Lebeau Aug 31 '13 at 20:45

2 Answers2

7

When an HTTP error occurs, TIdHTTP raises an EIdHTTPProtocolException exception. That exception contains the HTTP status code in its ErrorCode property, the HTTP status text in its Message property, and the response data in its ErrorMessage property.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thanks for answer. when I disabled hoNoProtocolErrorException ErrorMessage gave me response data. – Someone Sep 01 '13 at 13:42
3

try this code

Try
    Memo1.Text := IdHTTP1.post(url,data);
except on e: EIdHTTPProtocolException do
begin
    memo1.lines.add(idHTTP1.response.ResponseText);
    memo1.lines.add(e.ErrorMessage);
end;

e.ErrorMessage will give you some informations about the bad request.

TheWesDias
  • 293
  • 2
  • 8