如何使用Delphi检索Cloudant数据库中的附件?

时间:2017-08-18 15:46:02

标签: delphi https ibm-cloud indy cloudant

我已成功创建了一个Bluemix Cloudant文档,并使用Delphi程序为该文档添加了附件。我也可以通过BAT文件检索该附件,但我想用Delphi 2009来做。我得到了 一个HTTP / 1.1 200 OK响应,我不知道从哪里获得附件 代码:

  program httpsGet;

  {$R *.res}
  {$APPTYPE CONSOLE}

  uses
    IdHTTP, IdSSLOpenSSL, IdGlobal, SysUtils,IniFiles, Classes;

  var
    HTTP: TIdHTTP;
    RequestBody: TStream;
    ResponseBody: string;
    LHandler: TIdSSLIOHandlerSocketOpenSSL;
    json: string;
    F:  textfile;
    s:  string;
    id,rev: string;
    Dir,user,password,mapp,butik: string;
    IniFile:  TInifile;
    a,i,x,Antdocs:  integer;
  begin
  //  Read ini-file and get user / password
    Dir:=ExtractFilePath(ParamStr(0));
    IniFile:=TIniFile.create(Dir + 'Credentials.ini');
    user:=IniFile.Readstring('Credentials','User','');
    password:=IniFile.Readstring('Credentials','Password','');
    IniFile.Destroy;

    id:='02d42fc7c5966b2ab7d27a3c9942a77e';
    rev:='2-904b6ca2f8883e35b931d138acc969c6';
    json:='{ "selector": {"_id":"' + id + '"}, "fields": "_attachment" } ';

    HTTP := TIdHTTP.Create;
    try
      try
        RequestBody := TStringStream.Create(json, TEncoding.UTF8);
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

        try
          WriteLn(' RequestBody: ' + json);
          WriteLn('');
          HTTP.Request.BasicAuthentication:= true;
          HTTP.ConnectTimeout:= 10000;
          HTTP.Request.UserName := user;
          HTTP.Request.Password := password;
          HTTP.Request.Accept := 'application/json';
          HTTP.Request.ContentType := 'application/json';
          HTTP.IOHandler:=LHandler;
          ResponseBody := HTTP.Get('https://' + user + '.cloudant.com/pmu/');
          WriteLn(' ResponseBody: ' + ResponseBody);
          WriteLn(HTTP.ResponseText);
        finally
          RequestBody.Free;
          Lhandler.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.

输出是:

  

RequestBody:{" selector":{" _id":" 02d42fc7c5966b2ab7d27a3c9942a77e"},   " fields":" _attachment" } ResponseBody:   {" update_seq":" 331-g1AAAAPHeJzLYWBgEMhgTmFQSUlKzi9KdUhJMjTQS8rVTU7WLc3WNTDUS87JL01JzCvRy0styQGqZkpkSOL ___ 9_ViIHSJ8yQh9-bUkCQDJJHqxTAFWnGQGdCiCd-mCdoqg6jQjoNADptAfr5CTNl0kOIK3-YK3iqJZaEtAZANIZD9YpSZpHE0A688E6hUjzaAFIZz1YJz9pHs1jAZIMDUAKqLsfI4QJeBaiewJE9_ysRHlU3ebE6F4A0b0-K1GaJE9DdG-A6N6flahIjscPQLSfJzWWIbovQHTfJzWmIbofQHS_z0rkJcPjHyC6QekzCwCYMTXF"" DB_NAME":" PMU",& #34;尺寸" {"文件":9239940,"外部":2290914,"有源":2392413}," purge_seq&#34 ;:0,"其他" {" DATA_SIZE":2290914}," doc_del_count" 130" doc_count":2&# 34; DISK_SIZE":9239940," disk_format_version":6," DATA_SIZE":2392413," compact_running":假," instance_start_time" :" 0"}   HTTP / 1.1 200确定

我原以为附件是ResponseBody。 Cloudant doc如下所示: Doc layout for specified id

任何人都可以给我一个提示吗? 此致

1 个答案:

答案 0 :(得分:0)

您没有找到正确的网址。

我可以看到两个问题:考虑到如何构建json有效负载,您正在尝试使用Cloudant Query。它需要是/$DATABASE/_find端点上的POST,而您只是在/$DATABASE上发出GET。参见

https://console.bluemix.net/docs/services/Cloudant/api/cloudant_query.html#finding-documents-by-using-an-index

其次,附件存储在Cloudant中的方式有​​两种,可以是单独的blob,也可以是内联内容。访问附件取决于您使用的风格。

https://console.bluemix.net/docs/services/Cloudant/api/attachments.html#attachments

了解更多详情。