7
votes

I want to use pure http requests to get the result from google cloud natural language api, but their document does not specify the parameter names.

Here is my python code:

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)

Here is the error message: {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'}]}}

how should I create the http request without using their python library?

1

1 Answers

9
votes

ok, I figured it out. I need to pass JSON-Encoded POST/PATCH data, so the request should be r = requests.post(url, params=para, json=d)