You need to enable JavaScript to run this app. 使用HTTParty调用Akismet comment-check接口,传blog参数仍报值为空错误
优惠活动
大模型
产品
解决方案
定价
更多

使用HTTParty调用Akismet comment-check接口,传blog参数仍报值为空错误

报错成因

Akismet 的 comment-check 接口仅支持标准application/x-www-form-urlencoded格式的表单参数提交,不兼容JSON格式请求体。
当前代码存在两个关键问题直接触发报错:

  • 请求格式错配:手动对payload调用to_json将参数序列化为JSON字符串,却将请求头Content-Type声明为表单格式。服务端按照表单规则解析请求体时,完全无法识别JSON字符串里的键值对,因此判定blog字段值为空。
  • 参数拼写错误:代码中的peramlink不是Akismet接口认可的合法字段名,正确拼写为permalink,后续可能触发其他参数校验类报错。
修复方案

按照接口要求调整请求构造逻辑,无需手动序列化JSON、无需手动设置请求头,直接将哈希格式的payload作为body传入即可:

url = "https://my_akismet_key.rest.akismet.com/1.1/comment-check"

payload = {
  blog: "http://mysitename.com",
  comment_author: "Some Random User Name",
  comment_author_email: "example_1@gmail.com",
  comment_content: "Any comment",
  user_ip: "A Valid user ip",
  user_agent: "A Valid User agent",
  permalink: "A valid permalink",
  comment_type: "comment"
}

response = HTTParty.post(url, body: payload)

调整后HTTParty会自动按照urlencoded规则编码所有参数、自动带上匹配的Content-Type请求头,Akismet服务端可正常读取所有传入字段,不会再出现blog值为空的报错。

内容的提问来源于stack exchange,提问作者Vishal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.13 11:54:06