I'm working on python client examples with Cloudant NoSQLDB. For relatively simple document creation, it's OK. However, trying to upload a base64 encoded file as an attachment to doc, I run into a problem that I cannot solve by myself.
Please help me.
Here is my code.
from cloudant.client import Cloudant
from cloudant.error import CloudantException
from cloudant.result import Result,ResultByKey
import base64
.
.
.
client.connect()
databaseName = "mydata1"
myDatabase = client[databaseName]
targetfile = "chibitest.png"
with open(targetfile,"rb") as fp:
byte_content = fp.read()
dataContentb= base64.b64encode(byte_content)
dataContent = dataContentb.decode()
jsonDoc = {
"nameField": "sample1",
"_attachements":{
targetfile:
{"content-type":"image/png",
"data":dataContent}
}
}
newDocument = myDatabase.create_document(jsonDoc)
The result Error is as follows:
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request doc_validation Bad
special document member: _attachements for url:https://.....cloudant.com/mydata1
(without "_attachements" section , it works appropriately.)
Additional information.
In the above code, the line;
dataContent = dataContentb.decode()
is the one I needed to solve json error. This is thanks to an article, Serialize in JSON a base64 encoded data
Although, I could not specify "ENCODING" as decode() parameter as is shown in this article (because causing "not defined error"), I guess the resultant dataContent would be OK as far as I can see by inserting print statement..