如何修复 "Invalid JSON payload received. Unknown name" 错误
How to fix the "Invalid JSON payload received. Unknown name" error
我尝试从 Google 安全浏览 API v4 中获得正确的响应。虽然我收到错误 "Invalid JSON payload received. Unknown name".
我使用了基于 https://developers.google.com/safe-browsing/v4/lookup-api
中提到的有效载荷示例的有效载荷
我的负载有问题。我认为它应该是一个字符串,而不是一个真正的字典。当我使用字典时出现错误:TypeError: has type , but expected one of: 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]
}
]
}
]
}
}
有什么想法,有什么问题吗?
谢谢
urlfetch 应包含 HEADER Content-Type: application/json
result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload, headers={'Content-Type': 'application/json'})
我尝试从 Google 安全浏览 API v4 中获得正确的响应。虽然我收到错误 "Invalid JSON payload received. Unknown name".
我使用了基于 https://developers.google.com/safe-browsing/v4/lookup-api
中提到的有效载荷示例的有效载荷我的负载有问题。我认为它应该是一个字符串,而不是一个真正的字典。当我使用字典时出现错误:TypeError: has type , but expected one of: 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]
}
]
}
]
}
}
有什么想法,有什么问题吗?
谢谢
urlfetch 应包含 HEADER Content-Type: application/json
result = urlfetch.fetch(url, method=urlfetch.POST, payload=payload, headers={'Content-Type': 'application/json'})