https - How can I retrieve an attachment in a Cloudant database with Delphi? -
i have created bluemix cloudant document , did put attachment doc delphi program. i'm able retrieve attachment via bat-file, should delphi 2009. http/1.1 200 ok response, don't know attachment code:
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 , 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); requestbody.free; lhandler.free; end; except on e: eidhttpprotocolexception begin writeln(e.message); writeln(e.errormessage); end; on e: exception begin writeln(e.message); end; end; http.free; end; readln; reportmemoryleaksonshutdown := true; end. output is:
requestbody: { "selector": {"_id":"02d42fc7c5966b2ab7d27a3c9942a77e"}, "fields": "_attachment" } responsebody: {"update_seq":"331-g1aaaaphejzlywbgemhgtmfqsulkzi9kduhjmjtqs8rvtu7wlc3wntdus87jl01jzcvry0styqgqzkpksol___9_viihsj8yqh9-bukcqdjjhqxtafwngqgdcicd-mcdoqg6jqjonadptafr5ctnl0koik3-yk3iqjzaetazanizd9ypszphe0a688e6hujzaafizz1yjz9phs1jazimduakqlsfi4qjebaiewje9_ysrhlu3ebe6f4a0b0-k1gaje9ddg-a6n6flahijscpqlsfjzwwibovqhtfjzwmibofqhs_z0rkjcpjhyc6qekzcwcymtxf","db_name":"pmu","sizes":{"file":9239940,"external":2290914,"active":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 ok
i have expected attachment responsebody. cloudant doc looks follows: doc layout specified id
can give me hint? regards
you're not hitting right url.
two issues can see: you're trying use cloudant query, given how you've constructed json payload. needs post on /$database/_find endpoint, whereas you're issuing on /$database. see
secondly, there 2 ways attachments stored in cloudant, either separate blob, or inline content. access attachments depends on flavour you're using.
see
https://console.bluemix.net/docs/services/cloudant/api/attachments.html#attachments
for more details.
Comments
Post a Comment