1

Import Watson Developer Cloud Python SDK

from watson_developer_cloud import DiscoveryV1

Get the pdf from Slack doc_url which is the private URL

r = requests.get(doc_url, headers={'Authorization': 'Bearer {}'.format(slack_token) })
logging.debug("read_pdf headers %s " %r.headers )
logging.debug("read_pdf content-type %s " %r.headers['content-type'] )

Save the file in the cloud file system temporarily

with open(doc_name, 'wb' ) as f:
  f.write(r.content)
filepath = os.path.join(os.getcwd(), '.', doc_name )
logging.debug('filepath %s' %filepath)
logging.debug('filepath assertion %s' %os.path.isfile(filepath) )

Create a Discovery instance

discovery = DiscoveryV1(
username=DS_USERNAME,
password=DS_PASSWORD,
version="2017-10-16"
)

Add pdf document in Discovery instance

with open(filepath, 'rb') as fileinfo:
  add_doc = discovery.add_document(ENVIRONMENT_ID, COLLECTION_ID, file_content_type=r.headers['content-type'])

Log files

read_pdf headers {'Content-Type': 'application/pdf', 'Content-Length': '149814'
WatsonApiException: Error: Invalid Content-Type. Expected 'multipart/form-data', got 'application/octet-stream', Code: 400 , X-dp-watson-tran-id: gateway02-732476861 , X-global-transaction-id: ffea405d5ba1ad632ba8b5bd

Developer code examples are commented out in Github.

https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/discovery_v1.py

Carlos Ferreira
  • 1,980
  • 2
  • 14
  • 18

1 Answers1

3

Oh, my. That is a miserable error message.

What is missing from the call to discovery.add_document() is the file parameter. Can you try adding file=fileinfo like this:

with open(filepath, 'rb') as fileinfo:
  add_doc = discovery.add_document(ENVIRONMENT_ID,
                                   COLLECTION_ID,
                                   file=fileinfo,
                                   file_content_type=r.headers['content-type'])

For reference, here is some Python code that works and is doing something very similar to what it looks like you are aiming for.

Bruce Adams
  • 443
  • 2
  • 5