在Indy POST之后访问JSON数据

时间:2019-02-01 08:07:40

标签: http delphi indy

我正努力了解如何访问对Indy POST请求的响应。我将数据发布为JSON或paramstring。我使用JSON时的代码如下。

params := TStringList.Create;
try
  params.Text :=
    '{'
    + format ('"client_secret":"%s",', [FilesFrm.ClientSecret])
    + format ('"client_id":"%s",', [FilesFrm.ClientId])
    + '"grant_type":"authorization_code",'
    + '"redirect_uri":"http://localhost:8080",'
    + format ('"code":"%s"', [fCode])
    + '}';
  idLogFile1.Active := true;
  // Make sure it uses HTTP 1.1, not 1.0
  IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol];
  IdHTTP1.Request.ContentType := 'application/json';
  IdHttp1.Request.Accept := 'application/vnd.hmrc.1.0+json';

  try
    result := IdHTTP1.Post (
      'https://test-api.service.hmrc.gov.uk/oauth/token',
      params);
  except
    on E: Exception do
    memo1.lines.add (E.ClassName + ': ' + E.message);
    end;

  memo1.Lines.add (result);
  memo1.Lines.add (idHTTP1.ResponseText);
finally
  params.free;
  end;

打印结果和RepsonseText的结果就是

EIdHTTPProtocolException: HTTP/1.1 400 Bad Request

HTTP/1.1 400 Bad Request

但是,由于我在TidHTTP上附加了一个TidLogFile组件,因此可以看到实际到达的内容,如下所示。

Recv 2/1/2019 7:56:07 AM: HTTP/1.1 400 Bad Request<EOL>
Content-Type: application/json<EOL>
Content-Length: 72<EOL>
Cache-Control: no-cache,no-store,etc, etc...
; Secure; HTTPOnly<EOL><EOL>
{"error":"invalid_request","error_description":"grant_type is required"}

除了Grant_type似乎在原始请求数据中这一事实之外,我希望能够在最后访问JSON响应,因为“ grant_type_is_required”比“ Bad request”更有用,但是我不能找到它的位置。

我随后找到了包含值72的Response.ContentLength和理论上应包含72字节数据的Response.ContentStream,但是当我尝试提取数据时会产生访问冲突。

len := idHTTP1.Response.ContentLength;
memo1.Lines.Add(format ('Length = %d', [len]));
if assigned (idHTTP1.Response.ContentStream) then
  begin
  //idHTTP1.Response.ContentStream.Position := 0;
  result := ReadStringFromStream (idHTTP1.Response.ContentStream, len);
  end;
memo1.lines.add (result);

2 个答案:

答案 0 :(得分:3)

除了mjn42的答案在技术上是正确的之外,TIdHTTP在其hoNoProtocolErrorException属性中还具有可选的hoWantProtocolErrorContentHTTPOptions标志,您可以启用这些标志来避免分别引发EIdHTTPProtocolException和用错误数据填充result变量:

params := TStringStream.Create(
  '{'
  + format ('"client_secret":"%s",', [FilesFrm.ClientSecret])
  + format ('"client_id":"%s",', [FilesFrm.ClientId])
  + '"grant_type":"authorization_code",'
  + '"redirect_uri":"http://localhost:8080",'
  + format ('"code":"%s"', [fCode])
  + '}',
  TEncoding.UTF8);
try    
  IdLogFile1.Active := true;

  // Make sure it uses HTTP 1.1, not 1.0, 
  // and disable EIdHTTPProtocolException on errors
  IdHTTP1.ProtocolVersion := pv1_1;
  IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol, hoNoProtocolErrorException, hoWantProtocolErrorContent];

  IdHTTP1.Request.ContentType := 'application/json';
  IdHTTP1.Request.Accept := 'application/vnd.hmrc.1.0+json';

  try
    result := IdHTTP1.Post('https://test-api.service.hmrc.gov.uk/oauth/token', params);
  except
    on E: Exception do begin
      Memo1.Lines.Add(E.ClassName + ': ' + E.message);
      raise;
    end;
  end;

  Memo1.Lines.Add(result);
finally
  params.Free;
end;

答案 1 :(得分:1)

这里是example,显示了如何访问HTTP正文。

如果遇到EIdHTTPProtocolException异常,则可以访问正文。

  on E: EIdHTTPProtocolException do
  begin
    WriteLn(E.Message);
    WriteLn(E.ErrorMessage);
  end;

完整example code

program JSONPostExample;

{$APPTYPE CONSOLE}

uses
  IdHTTP, IdGlobal, SysUtils, Classes;

var
  HTTP: TIdHTTP;
  RequestBody: TStream;
  ResponseBody: string;
begin
  HTTP := TIdHTTP.Create;
  try
    try
      RequestBody := TStringStream.Create('{"日本語":42}',
        TEncoding.UTF8);
      try
        HTTP.Request.Accept := 'application/json';
        HTTP.Request.ContentType := 'application/json';
        ResponseBody := HTTP.Post('https://httpbin.org/post',
          RequestBody);
        WriteLn(ResponseBody);
        WriteLn(HTTP.ResponseText);
      finally
        RequestBody.Free;
      end;
    except
      on E: EIdHTTPProtocolException do
      begin
        WriteLn(E.Message);
        WriteLn(E.ErrorMessage);
      end;
      on E: Exception do
      begin
        WriteLn(E.Message);
      end;
    end;
  finally
    HTTP.Free;
  end;
  ReadLn;
  ReportMemoryLeaksOnShutdown := True;
end.

请注意,不得将TStringList用于POST正文。该版本的TIdHTTP.Post()根据application / x-www-form-urlencoded媒体类型格式化数据,该媒体类型不适用于JSON并会破坏它。