1

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 Answer 1

1

Use the json parameter instead of data:

x = requests.post(url, json=myobj)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.