I'm sending JSON to a restful service using TidHTTP.Post but having trouble reading the response from idHTTP when an HTTP error 400 occurs. When a 400 occurs, the server provides JSON which describes errors in the data being sent. I get readable JSON results sometimes but most of the time the response contains only a couple of unprintable characters.
procedure TForm1.SendData(const stlACSchedule: TStringList; BatchTimeIn: TDateTime);
var
TargetURL : String;
stsJson: TStringStream;
myResponse : String;
resp : TMemoryStream;
begin
TargetURL := 'http://sandbox.xxxxxxxxxxx.com/api/v1/feeds' ;
stsJson := TStringStream.Create;
stsJson.WriteString(stlACSchedule.Text);
resp := TMemoryStream.Create;
application.ProcessMessages;
try
myResponse := IdHTTP1.Post( TargetURL, stsJson );
WriteStatus( 'Response from Vendor Server: ' );
UpdateUploadStatus2( IdHTTP1.ResponseCode, IdHttp1.ResponseText, BatchTimeIn, 'usPending', 'usUploaded' );
except
on E: EIdHTTPProtocolException do begin
WriteStatus( 'HTTP Protocol Error from Vendor Server: ' + #13 + E.ErrorMessage + ' -*- ' + IdHTTP1.ResponseText );
UpdateUploadStatus2( IdHTTP1.ResponseCode, E.ErrorMessage + ' - ' + IdHTTP1.ResponseText, BatchTimeIn, 'usPending', 'usBatchFail' );
ShowMessage('Fubar_!: ' + myResponse);
end;
on E: Exception do begin
WriteStatus( 'Unknown Error from Vendor Server:' );
UpdateUploadStatus2( IdHTTP1.ResponseCode, E.Message + ' - ' + IdHTTP1.ResponseText, BatchTimeIn, 'usPending', 'usBatchFail' );
ShowMessage('Fubar!: ' + myResponse);
end;
end;
resp.free;
stsJson.free;
end; { SendData }
procedure TForm1.WriteStatus(strTextIn : String);
begin
Memo1.Lines.add('');
Memo1.Lines.add(strTextIn);
Memo1.Lines.add(IntToStr(IdHTTP1.ResponseCode));
Memo1.Lines.add(IdHTTP1.ResponseText);
end; { WriteStatus }
It appears that the EIdHTTPProtocolException exception handler is catching the error but most of the time I get responses like this (there are 3 unprintable characters below the "HTTP Protocol Error from Vendor Server" line):
HTTP Protocol Error from Vendor Server:
400
HTTP/1.1 400 Bad Request
but occasionally I get a good response, like this:
HTTP Protocol Error from ReturnJet Server:
{"errors":[{"code":7,"message":"Invalid departure or destination type for event: 1"}]} -*- HTTP/1.1 400 Bad Request
400
HTTP/1.1 400 Bad Request
It looks like it may have something to do w/ the length of the response but I'm not sure.
What do I need to do to consistently decode the ResponseText?
I'm using: Delphi XE8, Indy ver 10.6
PS - when I Post this data manually using Postman, I always get the full JSON response.
TIA