Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 在Indy POST之后访问JSON数据_Http_Delphi_Indy - Fatal编程技术网

Http 在Indy POST之后访问JSON数据

Http 在Indy POST之后访问JSON数据,http,delphi,indy,Http,Delphi,Indy,我很难知道如何访问对印地邮报请求的回复。我将数据发布为JSON或paramstring。我使用JSON时的代码如下 params := TStringList.Create; try params.Text := '{' + format ('"client_secret":"%s",', [FilesFrm.ClientSecret]) + format ('"client_id":"%s",', [FilesFrm.ClientId]) + '"grant

我很难知道如何访问对印地邮报请求的回复。我将数据发布为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
但是,因为我有一个TidLogFile组件连接到TidHTTP,所以我可以看到实际到达的内容,如下所示

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"}
下面是一个示例,展示了如何访问HTTP主体

如果捕获EIdHTTPProtocolException异常,则可以访问主体

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


请注意,您不能在帖子正文中使用TStringList。该版本的TIdHTTP.Post()根据application/x-www-form-urlencoded media type格式化数据,该类型不适用于JSON,并且会损坏它。

除了mjn42的答案,该答案在技术上是正确的,
TIdHTTP
在其
HTTPOptions
属性中还具有可选的
hoNoProtocolErrorException
hoWantProtocolErrorContent
标志,您可以启用这些标志,以避免引发
eidttpprotocolexception
,并分别用错误数据填充
结果
变量:

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;

谢谢mjn42。我现在已经按照建议实现了错误捕获,现在可以访问错误代码了。更重要的是你最后的笔记。通过使用TStringStream,我已经能够使用多行正确格式化JSON,并且“grant_type is required”问题已经解决,导致web服务器使用正确的数据重放。相关:,
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.
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;