谷歌云自然语言api的参数

时间:2016-10-13 06:39:24

标签: python rest google-api google-cloud-platform

我想使用纯http请求从google cloud自然语言api获取结果,但是他们的文档没有指定参数名称。

这是我的python代码:

import requests
url = "https://language.googleapis.com/v1beta1/documents:analyzeEntities"
d = {"document": {"content": "some text here", "type": "PLAIN_TEXT"}}
para = {"key": "my api key"}
r = requests.post(url, params=para, data=d)

以下是错误消息: {u'error': {u'status': u'INVALID_ARGUMENT', u'message': u'Invalid JSON payload received. Unknown name "document": Cannot bind query parameter. \'document\' is a message type. Parameters can only be bound to primitive types.', u'code': 400, u'details': [{u'fieldViolations': [{u'description': u'Invalid JSON payload received. Unknown name "document": Cannot bind query parameter. \'document\' is a message type. Parameters can only be bound to primitive types.'}], u'@type': u'type.googleapis.com/google.rpc.BadRequest'}]}}

如何在不使用python库的情况下创建http请求?

1 个答案:

答案 0 :(得分:5)

好吧,我明白了。我需要传递JSON编码的POST / PATCH数据,因此请求应为r = requests.post(url, params=para, json=d)