2016-10-13 75 views
2

我想使用純HTTP請求來從谷歌雲自然語言API中得到結果,但他們的文檔沒有指定參數名稱。谷歌雲自然語言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請求?

回答

3

好吧,我想通了。我需要通過JSON編碼的POST/PATCH數據,所以請求應該是r = requests.post(url, params=para, json=d)