0

I’m struggling on how to retrieve an attachment from a CouchDB in Server-Swift.

I understand that CouchDB has a kind of ‘flag’ (called stub) to indicate that there is an attachment. In the JSON structure I get back from CouchDB I can spot under the key “_attachments” metadata (Content-Type, length etc)

I know how to retrieve that attachment from the CLI (curl -X GET ip:port{id}/{attachment_file}) but I’m drawing a blind how to do it from Swift.

The code to go through the results of a database.retrieveAll() call:

    if let docs = docs {
    for document in docs["rows"].arrayValue {
        var plaatje = [String: Any]()
       plaatje["ordernum"]  = document["doc"]["orderNumber"].stringValue                    
       plaatje["img"] = // what to put here?????  

          plaatjes.append(plaatje)                          
    }
}

Is there a method of the database instance or any other way to get the binary of the attachment from Swift?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
thewall
  • 53
  • 6
  • Which library are you using to access CouchDB? Or are you just making straight API calls yourself? – Jonathan Hall Sep 04 '18 at 08:19
  • I've imported the Kitura-CouchDB repo. I can't use the REST APIs (server ports are restricted and port 5984 is not accessible from the client) and want to get hold of the attachment from within my Swift code and pack it in a JSON and send back to the client... – thewall Sep 04 '18 at 11:20
  • Aha, perhaps I'm not clear. Sorry. On the server I've installed CouchDB and on the same server my Serverside Swift code is running. From the Swift code I can retrieve documents from the CouchDB, but I draw a blank on how to retrieve an attachment in the CouchDB (being an image) – thewall Sep 04 '18 at 11:23
  • Makes more sense now. So you're using Kitura-CouchDB to talk to CouchDB? Does it not offer the option to fetch attachments? – Jonathan Hall Sep 04 '18 at 11:25
  • Not that I can find. There are some samples but all is limited to create, add, retrieve documents, but I can't find any option for fetching the attachment from the swift code. Typically the solution is to retrieve the _id from swift and send that back to the client and create a REST call with the _id and perform a second call directly to CouchDB with the .../id/ but I would like to retrieve it from my code... – thewall Sep 04 '18 at 11:30
  • A cursory glance around the Kitura repo suggests that the attachment API is present: https://github.com/IBM-Swift/Kitura-CouchDB/blob/master/Sources/CouchDB/Database.swift#L564 – xpqz Sep 05 '18 at 13:06
  • Aha, indeed! Working on server side swift with a simple text-editor instead of the Xcode IDE (with suggestion and code-completion), I missed this option. I will check the githb ropes in the future. Many thanks. – thewall Sep 09 '18 at 20:36

0 Answers0