【问题标题】:Failed to translate JSON in request error in Watson discovery query call在 Watson 发现查询调用中的请求错误中无法转换 JSON
【发布时间】:2021-12-08 16:11:17
【问题描述】:

使用ibm-watson Python SDK 5.3.0 版和以下代码示例:

from ibm_watson import DiscoveryV2, ApiException
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import json


url = 'https://api.eu-de.discovery.watson.cloud.ibm.com'
version = '2020-08-30'

apikey = '...'
project = '...'
collection = '...'

authenticator = IAMAuthenticator(apikey)
discovery = DiscoveryV2(version=version, authenticator=authenticator)
discovery.set_service_url(url)

try:
    result = discovery.query(
        project_id=project,
        collection_ids=collection,
        query='text:searchterm',
        table_results=True
    ).get_result()


    print(json.dumps(result, indent=2))

except ApiException as ex:
    print(f'Failed. Status code {ex.code}: {ex.message}')

从服务器返回以下错误。

失败。状态码 400:无法在请求中转换 JSON:不能 反序列化类型的值 `scala.collection.immutable.List` 来自字符串值 (令牌`JsonToken.VALUE_STRING`)

只有在指定了collections_idstable_results 时才会发生错误。

【问题讨论】:

    标签: python-3.x watson-discovery


    【解决方案1】:

    服务器告诉您,发送给它的 JSON 与它期望处理的不匹配。

    API 请求的collections_idstable_results 格式不正确。

    根据API documentation

    • collections_ids 必须是一个列表。所以改成collections_ids=[collections]
    • table_results 需要一个对象。正确使用table_results={ 'enabled': True }

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2021-04-11
      • 2021-10-11
      相关资源
      最近更新 更多