如何修复“收到无效的JSON有效负载。未知名称”错误

时间:2019-04-25 18:39:01

标签: google-app-engine safe-browsing-api

我尝试从Google安全浏览API v4得到适当的答复。虽然我收到错误消息“接收到无效的JSON有效负载。未知名称”。

我已根据https://developers.google.com/safe-browsing/v4/lookup-api

提到的有效负载示例使用了有效负载

我的有效载荷有问题。我认为应该是一个字符串,而不是一个真正的字典。当我使用字典时,出现错误:TypeError:具有type,但预期为以下之一:str,unicode

我使用的代码是:

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

URL是(带有[api-key]当然是我的api-key):

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

有效负载是以下字符串(不是python字典):

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

作为输出,我期望一些JSON表示此URL是安全的。但是我得到以下结果:

{ 
  "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]
          }
        ]
      }
    ]
  }
}

任何想法,怎么了?

谢谢

1 个答案:

答案 0 :(得分:0)

urlfetch应该包含HEADER内容类型:application / json

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