1
votes

I'm trying to perform sentiment analysis using google NLP cloud API.

Below is my code

import requests

url = "https://language.googleapis.com/v1/documents:analyzeSentiment"
myobj = {
    "key": "XYZ",
    "document":{
        "type":"PLAIN_TEXT",
        "language": "EN",
        "content":"'Lawrence of Arabia' is a highly rated film biography about British Lieutenant T. E. Lawrence. Peter O'Toole plays Lawrence in the film."
      },
    "encodingType":"UTF8",
    "Content-Type": "application/json"
}

x = requests.post(url, data = myobj)

print(x.text)

But it is giving me error

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"Content-Type\": Cannot bind query parameter. Field 'Content-Type' could not be found in request message.\nInvalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"Content-Type\": Cannot bind query parameter. Field 'Content-Type' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"document\": Cannot bind query parameter. 'document' is a message type. Parameters can only be bound to primitive types."
          }
        ]
      }
    ]
  }
}

Does anybody know why it is happening? Will appreciate any help. Thanks.

1

1 Answers

1
votes

Use the json parameter instead of data:

x = requests.post(url, json=myobj)