3
votes

I'm using the following code on Delphi XE7 to make requests to a REST API. It works well, except for when an error such as Internal Server Error occurs. On this occasion, StatusCode is 0 (while it should be 500) and Content only returns response header (while I need response body).

var
  RESTClient: TRESTClient;
  RESTRequest: TRESTRequest;
begin
 try
  RESTClient:= TRESTClient.Create('http://blah.example.com');
  RESTRequest:= TRESTRequest.Create(nil);
  RESTRequest.Method:= TRESTRequestMethod.rmGET;
  RESTRequest.Resource:= 'customers';
  RESTRequest.Accept:= 'application/json';
  RESTRequest.Client:= RESTClient;
  RESTRequest.Execute;
 finally
  RESTClient.Free;
  RESTRequest.Free;
 end;

Everything looks okay in Fiddler. How can I get actual status code and response body when an error (such as Internal Server Error) happens?

2

2 Answers

2
votes
function Funcion(BaseURL: string; UrlAuth: string; Usuario: string; Password: string): string;
var
    RESTClient   : TRESTClient;
    RESTRequest  : TRESTRequest;
    RESTResponse : TRESTResponse;
    JSONMensaje  : TJSONObject;
    JSONResponse : TJSONObject;
    jValue       : TJSONValue;
    Token        : string;
begin
    Result := '';
    RESTClient := TRESTClient.Create(nil);
    RESTRequest := TRESTRequest.Create(nil);
    JSONMensaje := TJSONObject.Create;
     try
        Result := '';
        RESTClient.BaseURL := BaseURL;
        RESTClient.Accept  := 'application/json';
        RESTClient.AcceptCharSet := 'UTF-8';
        RESTClient.ContentType   := 'application/json';
        RESTClient.RaiseExceptionOn500 := true;
        // Se inicia el mensaje JSON a enviar
        JSONMensaje.AddPair('username', Usuario);
        JSONMensaje.AddPair('password', Password);

        RESTRequest.Method := TRESTRequestMethod.rmPOST;
        RESTRequest.Client := RESTClient;

        RESTClient.Params.Clear;
        RESTRequest.Params.Clear;
        RESTRequest.ClearBody;
        RESTRequest.Resource := UrlAuth;
        RESTRequest.Params.AddItem('', JSONMensaje.ToString, pkREQUESTBODY, [poDoNotEncode],
              TRESTContentType.ctAPPLICATION_JSON);

        RESTResponse := TRESTResponse.Create(nil);
        RESTRequest.Response := RESTResponse;

        RESTRequest.Accept := 'application/json';
        try
          RESTRequest.Execute;
          if Assigned(RESTResponse.JSONValue) then
          begin
            jValue := RESTResponse.JSONValue;

            // Parsear el JSON
            JSONResponse := TJSONObject.Create;
            JSONResponse := RESTResponse.JSONValue as TJSONObject;
            if RESTResponse.StatusCode = 200 then
            begin
              if Assigned(JSONResponse.GetValue('valor')) then
              begin
                 Token := JSONResponse.GetValue('valor').ToString;
              end
            end
            else
            begin
              if Assigned(JSONResponse.GetValue('error')) then
              begin
                 Result := 'Error: ' + JSONResponse.GetValue('error').ToString;
              end;
            end;
            RESTResponse.Free;
            JSONResponse.Free;
          end;
        except on E:Exception do
          begin
            Result := 'Error: ' + RESTResponse.ErrorMessage + ' Error: ' + E.Message;
          end;
        end;
    finally
       RESTClient.Free;
       RESTRequest.Free;
       JSONMensaje.Free;
    end;
end;
1
votes

RESTClient has a property RaiseExceptionOn500 which may help to catch this error