【问题标题】:Get Response Body and Status Code when TRESTRequest Encounters ErrorTRESTRequest 遇到错误时获取响应正文和状态码
【发布时间】:2015-05-23 08:19:26
【问题描述】:

我在 Delphi XE7 上使用以下代码向 REST API 发出请求。它运行良好,除非发生内部服务器错误等错误。在这种情况下,StatusCode 是 0(而它应该是 500),Content 只返回响应头(而我需要响应体)。

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;

在 Fiddler 中一切正常。发生错误(例如内部服务器错误)时,如何获取实际状态码和响应正文?

【问题讨论】:

    标签: rest delphi http-status-codes delphi-xe7


    【解决方案1】:
    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;
    

    【讨论】:

      【解决方案2】:

      RESTClient 有一个属性 RaiseExceptionOn500 可能有助于捕获此错误

      【讨论】:

        猜你喜欢
        • 2013-02-27
        • 2015-02-20
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        • 2020-06-26
        相关资源
        最近更新 更多