如何修复收到的“无效JSON有效负载".未知名称"错误 [英] How to fix the "Invalid JSON payload received. Unknown name" error

查看:82
本文介绍了如何修复收到的“无效JSON有效负载".未知名称"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Google安全浏览API第4版获得适当的响应.虽然我收到错误消息接收到无效的JSON有效负载.未知名称".

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".

我已根据 https:/中提到的有效负载示例使用了有效负载/developers.google.com/safe-browsing/v4/lookup-api

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

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

我使用的代码是:

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

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

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

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

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

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

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

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

任何想法,怎么了?

谢谢

推荐答案

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

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

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

这篇关于如何修复收到的“无效JSON有效负载".未知名称"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆