【问题标题】:How can I retrieve an attachment in a Cloudant database with Delphi?如何使用 Delphi 在 Cloudant 数据库中检索附件?
【发布时间】:2017-08-18 15:46:02
【问题描述】:

我已成功创建了一个 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"}, “字段”:“_attachment”}响应体: { “update_seq”: “331-g1AAAAPHeJzLYWBgEMhgTmFQSUlKzi9KdUhJMjTQS8rVTU7WLc3WNTDUS87JL01JzCvRy0styQGqZkpkSOL ___ 9_ViIHSJ8yQh9-bUkCQDJJHqxTAFWnGQGdCiCd-mCdoqg6jQjoNADptAfr5CTNl0kOIK3-YK3iqJZaEtAZANIZD9YpSZpHE0A688E6hUjzaAFIZz1YJz9pHs1jAZIMDUAKqLsfI4QJeBaiewJE9_ysRHlU3ebE6F4A0b0-K1GaJE9DdG-A6N6flahIjscPQLSfJzWWIbovQHTfJzWmIbofQHS_z0rkJcPjHyC6QekzCwCYMTXF”, “DB_NAME”: “PMU”, “尺寸”:{ “文件”:9239940, “外部”:2290914, “活性”: 2392413},"purge_seq":0,"other":{"data_size":2290914},"doc_del_count":130,"doc_count":2,"disk_size":9239940,"disk_format_version":6,"data_size":2392413 ,"compact_running":false,"instance_start_time":"0"} HTTP/1.1 200 正常

我预计附件是 ResponseBody。 Cloudant 文档如下所示: Doc layout for specified id

谁能给我一个提示? 问候

【问题讨论】:

    标签: delphi https ibm-cloud indy cloudant


    【解决方案1】:

    您没有点击正确的网址。

    我可以看到两个问题:鉴于您构建 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

    了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2011-07-26
      • 1970-01-01
      相关资源
      最近更新 更多