我尝试将此代码与Discovery API配合使用,但出现一个奇怪的错误。似乎发现无法识别我的简单HTML并拒绝了它。我尝试了很多简单的HTML文件,但没有用:
import sys import os import json from watson_developer_cloud import DiscoveryV1discovery = DiscoveryV1(version="2018-10-15", url='https://gateway.watsonplatform.net/discovery/api', username=username, password=password) with open((os.path.join(os.getcwd(), 'html_simple_file.html')), "r") as fileinfo: add_doc = discovery.add_document(Environment_Id, Collection_Id, file_info=fileinfo, file_content_type = "text/html") print(json.dumps(add_doc, indent=2))
--------------------------------------------------------------------------- WatsonApiException Traceback (most recent call last) in () 13 Collection_Id, 14 file_info=fileinfo, ---> 15 file_content_type = "text/html") 16 print(json.dumps(add_doc, indent=2))
~/anaconda3/lib/python3.7/site-packages/watson_developer_cloud/discovery_v1.py in add_document(self, environment_id, collection_id, file, metadata, file_content_type, filename, **kwargs) 1246 params=params, 1247 files=form_data, -> 1248 accept_json=True) 1249 return response 1250 ~/anaconda3/lib/python3.7/site-packages/watson_developer_cloud/watson_service.py in request(self, method, url, accept_json, headers, params, json, data, files, **kwargs) 488 error_info = self._get_error_info(response) 489 raise WatsonApiException(response.status_code, error_message, --> 490 info=error_info, httpResponse=response) WatsonApiException: Error: Invalid Content-Type. Expected 'multipart/form-data', got 'application/octet-stream', Code: 400 , X-dp-watson-tran-id: gateway01-30825332 , X-global-transaction-id: 7ecac92c5bf2f40901d65b74
我不知道它是如何工作的。谢谢您的帮助!
答案 0 :(得分:1)
问题与 open 调用有关,您需要在其中传递文件名(.html)的文件路径。这是工作代码
import os
import json
from watson_developer_cloud import DiscoveryV1
discovery = DiscoveryV1(
version="2018-10-15",
username='{USERNAME}',
password='{PASSWORD}',
#iam_apikey='{apikey}',
url='https://gateway.watsonplatform.net/discovery/api'
)
with open(os.path.join(os.getcwd(), '/Users/VMac/Downloads/', 'Ana.json.html')) as fileinfo:
add_doc = discovery.add_document('{DISCOVERY_ENVIRONMENT_ID}', '{DISCOVERY_COLLECTION_ID}', file=fileinfo).get_result()
print(json.dumps(add_doc, indent=2))
在上面的代码中传递了所有必需的详细信息之后,这就是输出
{
"document_id": "6439d5f2-f273-4173-8d25-54dc934df",
"status": "processing"
}
请参阅add_document here的API文档