narag 0 Posted yesterday at 01:11 PM Hello, I'm using TIdHttp to POST to a partner's HTTPS server. It takes raw JSON and answers also with JSON. The server guys return a 400 Bad Request for application validation errors. I don't think that's a good idea, but not sure they will accept my suggestion to change it. I've included hoNoProtocolException in HttpOptions. But still TIdHTTP.Post returns an empty string. The property ResponseText returns "HTTP 400 Bad Request" but testing with Postman I see: { "status": 400, "message": "Duplicate transaction reference. Please use a unique reference." } I need that JSON to know what exactly went wrong with the request. Tried using an output stream, but also got a blank result. Last incarnation of the code, looking in Response.ContentStream (also empty): function TXXXXConnection.Post(URL, Content: string): Boolean; var InStream, OutStream: TStringStream; Response: string; begin Http.Request.CustomHeaders.Values['x-client-id'] := XXXXXX; Http.Request.CustomHeaders.Values['x-client-secret'] := YYYYYY; Http.Request.ContentType := 'application/json'; Http.Request.Accept := '*/*'; Http.HttpOptions := Http.HttpOptions + [hoNoProtocolErrorException]; InStream := TStringStream.Create(Content); OutStream := TStringStream.Create(''); try OutputObject := SO(Http.Post(BaseURL + URL, InStream)); Result := Http.ResponseCode = 200; if not Result then begin OutStream.CopyFrom(Http.Response.ContentStream); OutputObject := SO('{"error": "' + OutStream.DataString + '"}'); end; finally InStream.Free; OutStream.Free; end end; It seems that the response JSON is discarded when status code is an HTTP error. Or can I access it using another property? Thanks in advance, Nico Share this post Link to post
Brian Evans 129 Posted yesterday at 01:27 PM (edited) Set HTTPOptions hoWantProtocolErrorContent. Refs: https://www.indyproject.org/2016/01/10/new-tidhttp-flags-and-onchunkreceived-event/ https://stackoverflow.com/questions/71341467/indy-error-in-delphi-7-undeclared-identifier-howantprotocolerrorcontent Edited yesterday at 01:30 PM by Brian Evans Share this post Link to post
narag 0 Posted yesterday at 01:54 PM 24 minutes ago, Brian Evans said: Set HTTPOptions hoWantProtocolErrorContent. Refs: https://www.indyproject.org/2016/01/10/new-tidhttp-flags-and-onchunkreceived-event/ https://stackoverflow.com/questions/71341467/indy-error-in-delphi-7-undeclared-identifier-howantprotocolerrorcontent That was it, thank you very much! It seems I was using a very outdated version of the docs... added in 2016. Share this post Link to post