Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi Indy Response Body bei EIdHTTPProtocolexception auswerten (https://www.delphipraxis.net/197095-indy-response-body-bei-eidhttpprotocolexception-auswerten.html)

Codehunter 17. Jul 2018 14:37

Indy Response Body bei EIdHTTPProtocolexception auswerten
 
Hallo!

Ich habe das Problem, dass ich mit Indy einen PUT-Request an einen REST-Service absetze. Wenn an dem Request etwas nicht stimmt, antwortet der Server mit einem HTTP-Statuscode 400 (Bad Request). Soweit auch noch ok.

Der TIdHTTP jedoch wirft dann jedoch eine EIdHTTPProtocolexception und gibt den Responsebody nicht zurück. Den bräuchte ich aber, weil darin in JSON-Form Details zum aufgetretenen Fehler enthalten sind.

Jemand eine Idee, wie ich in diesem Fall an den Responsebody kommen kann?

Grüße
Cody

HolgerX 17. Jul 2018 14:45

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
Hmm..

über die HTTPOptions:
(Auszug aus nem Source, bei dem ich auch selber machen will)

Delphi-Quellcode:
var
 O : TIdHTTPOptions;
...

    // Damit wir selber den ErrorCode verarbeiten können.
    O := Http.HTTPOptions;
    include(O, hoNoProtocolErrorException);
    include(O, hoWantProtocolErrorContent);
    Http.HTTPOptions := O;
(Verwende D6 mit Indy 10.6.2.0)

Codehunter 17. Jul 2018 14:52

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
Zitat:

Zitat von HolgerX (Beitrag 1407538)
Delphi-Quellcode:
var
 O : TIdHTTPOptions;
...

    // Damit wir selber den ErrorCode verarbeiten können.
    O := Http.HTTPOptions;
    include(O, hoNoProtocolErrorException);
    include(O, hoWantProtocolErrorContent);
    Http.HTTPOptions := O;
(Verwende D6 mit Indy 10.6.2.0)

Geht auch kürzer:

Delphi-Quellcode:
FHTTP.HTTPOptions := FHTTP.HTTPOptions + [hoNoProtocolErrorException, hoWantProtocolErrorContent];


Aber leider leider: FHTTP.Put liefert dann zwar keine Exception mehr, aber auch keinen Inhalt im Rückgabestring. Es ist aber Inhalt da, das habe ich mit apitester.com geprüft.

EDIT: Fehler gefunden. Dein Hinweis war richtig, nur wurden die HTTPOptions an anderer Stelle noch mal überschrieben. Funktioniert und Problem gelöst. Danke dir!

mjustin 17. Jul 2018 15:51

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
oder ohne die options zu verändern, indem man einen Handler für die EIdHTTPProtocolException schreibt in dem man auf die ErrorMessage zugreifen kann:

Delphi-Quellcode:
    try
      ...
    except
      on E: EIdHTTPProtocolException do
      begin
        WriteLn(E.Message);
        WriteLn(E.ErrorMessage);
      end;
      on E: Exception do
      begin
        WriteLn(E.Message);
      end;
    end;
Quelle: https://mikejustin.wordpress.com/201...-6-https-post/

Codehunter 17. Jul 2018 16:15

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
MessageBody <> ErrorMessage! Ersteres enthält JSON-Daten, letzteres "Bad Request". Aber siehe Edit oben: Problem gelöst.

mjustin 17. Jul 2018 17:23

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
Zitat:

Zitat von Codehunter (Beitrag 1407558)
MessageBody <> ErrorMessage! Ersteres enthält JSON-Daten, letzteres "Bad Request". Aber siehe Edit oben: Problem gelöst.

Der Responsebody (JSON-Daten) steht in E.ErrorMessage, und die Fehlermeldung ("Bad Request") in E.Message. Ich habe es nicht mit der aktuellen Indy-Version getestet, aber es war so als (alternative) Lösung von Remy Lebeau implementiert und hat funktioniert.

Codehunter 17. Jul 2018 18:41

AW: Indy Response Body bei EIdHTTPProtocolexception auswerten
 
Bei mir stand in der E.ErrorMessage das Bad Request drin, E.Message war leer. Aber egal, mit den Options funktioniert es wunderbar. Den Fehlercode cann ich ja genauso gut über TIdhTTP.ResponseCode abfragen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:05 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz