【问题标题】:Safebrowsing API returns 'Invalid JSON payload received'Safebrowsing API 返回“收到无效的 JSON 有效负载”
【发布时间】:2019-12-13 13:03:02
【问题描述】:

我正在使用安全浏览 API 从我的数据库中检查一些 URL,但请求给了我这个结果:

data {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"threatInfo[threatTypes][0]\": Cannot bind query parameter. Field 'threatInfo[threatTypes][0]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"threatInfo[threatTypes][1]\": Cannot bind query parameter. Field 'threatInfo[threatTypes][1]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"threatInfo[platformTypes][0]\": Cannot bind query parameter. Field 'threatInfo[platformTypes][0]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"threatInfo[threatEntryTypes][0]\": Cannot bind query parameter. Field 'threatInfo[threatEntryTypes][0]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"threatInfo[threatEntries][0][url]\": Cannot bind query parameter. Field 'threatInfo[threatEntries][0][url]' could not be found in request message."
  }
} 

我正在尝试以下代码:

const request = require('request');
    const body = {
      threatInfo: {
        threatTypes: ["SOCIAL_ENGINEERING", "MALWARE"],
        platformTypes: ["ANY_PLATFORM"],
        threatEntryTypes: ["URL"],
        threatEntries: [{url: "http://www.urltocheck2.org/"}]
      }
    }

    const options = {
      headers: {
        "Content-Type": "application/json"
      },
      method: "POST",
      url: "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${API_KEY}",
      form: body
    }

    console.log(options);
    request(options,
        function(err, res, data) {
            console.log('data', data)
            if (!err && res.statusCode == 200) {
              console.log(data);
            }
        }
    )

我预计请求的输出是 {},此示例中的状态代码为 200。

【问题讨论】:

  • setting form 将内容类型设置为 application/x-www-form-urlencoded - 这可能是问题 - 尝试使用 json: 而不是 form:
  • Content-type 是 application/json,但是在选项中你设置了'form'。下面是一个使用json创建请求的例子stackoverflow.com/a/27190736/426840

标签: javascript node.js safe-browsing-api


【解决方案1】:

如果你查看request() doc for the form property,你会看到:

form - 当传递一个对象或查询字符串时,这会将 body 设置为 value 的查询字符串表示,并添加 Content-type: application/x-www-form-urlencoded 标头。当不传递任何选项时,将返回一个 FormData 实例(并通过管道发送到请求)。请参阅上面的“表单”部分。

当您查看Google safe browsing API 时,您会看到:

POST https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY HTTP/1.1 内容类型:application/json

您正在发送 Content-type: application/x-www-form-urlencoded,但 API 需要 Content-Type: application/json。您需要发送 JSON,而不是形成编码数据。

您可以通过以下方式将form 属性更改为json 属性:

const options = {
  headers: {
    "Content-Type": "application/json"
  },
  method: "POST",
  url: "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${API_KEY}",
  form: body
}

到这里:

const options = {
  method: "POST",
  url: "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${API_KEY}",
  json: body     // <=== change here
}

内容类型会自动设置为匹配生成的正文的格式,因此您无需设置它。

【讨论】:

  • 像魅力一样工作,感谢您的解释和时间。
  • @J.Gandra - 有人用所有适当的代码写出一个干净、清晰的问题总是很好的。让我们的回答工作更轻松。
猜你喜欢
  • 1970-01-01
  • 2020-06-26
  • 2020-12-01
  • 1970-01-01
  • 2016-06-19
  • 2020-03-30
  • 2018-08-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多