google-app-enginesafe-browsing-api

How to fix the "Invalid JSON payload received. Unknown name" error


I try to get a proper response from the Google Safe Browsing API v4. Although I get the error "Invalid JSON payload received. Unknown name".

I have used a payload based on the payload example mentioned at https://developers.google.com/safe-browsing/v4/lookup-api

I have problems with the payload. I think it should be a string, not a real dict. When I use a dict I get the error: TypeError: has type , but expected one of: str, unicode

The code I used is:

result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload)

The url is (with [api-key] is of course my api-key):

https://safebrowsing.googleapis.com/v4/threatMatches:find?key=[api-key]

The payload is the following string (not a python dict):

{
  "client": { 
     "clientId": "myproject", 
     "clientVersion": "42" }, 
  "threatInfo": { 
     "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"], 
     "platformTypes": ["WINDOWS"], 
     "threatEntryTypes": ["URL"], 
     "threatEntries": [ {"url":"http://www.example.com/"} ] } 
}

As output I expected some JSON which indicates that this url is safe. However I get the following result:

{ 
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"{\n    \"client\": {\n      \"clientId\":      \"myproject\",\n      \"clientVersion\": \"42\"\n    },\n    \"threatInfo\": {\n      \"threatTypes\":      [\"MALWARE\", \"SOCIAL_ENGINEERING\"],\n      \"platformTypes\":    [\"WINDOWS\"],\n      \"threatEntryTypes\": [\"URL\"],\n      \"threatEntries\": [\n        {\"url\":\"http://www.example.com/\"}\n      ]\n    }\n  }\": Cannot bind query parameter. Field '{\n    \"client\": {\n      \"clientId\":      \"myproject\",\n      \"clientVersion\": \"42\"\n    },\n    \"threatInfo\": {\n      \"threatTypes\":      [\"MALWARE\", \"SOCIAL_ENGINEERING\"],\n      \"platformTypes\":    [\"WINDOWS\"],\n      \"threatEntryTypes\": [\"URL\"],\n      \"threatEntries\": [\n        {\"url\":\"http://www' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": [similar as the message above]
          }
        ]
      }
    ]
  }
}

Any ideas, what is wrong?

Thanks


Solution

  • The urlfetch should contain the HEADER Content-Type: application/json

    result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload, headers={'Content-Type': 'application/json'})