Explainers

CaptchaAI JSON API 与 Form API:使用哪种格式

CaptchaAI 接受表单编码和 JSON 格式的请求。两者的工作原理相同——根据您的语言和偏好进行选择。


并排比较

表单编码(默认)

import requests

resp = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
    "json": 1,
})

内容类型:application/x-www-form-urlencoded

JSON

import requests

resp = requests.post("https://ocr.captchaai.com/in.php", json={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
    "json": 1,
})

内容类型:application/json


主要差异

因素 形式编码 JSON
内容类型 application/x-www-form-urlencoded application/json
数据结构 平面键值对 可以嵌套对象
二进制数据 使用multipart进行文件上传 body 字段中的 Base64 编码
阵列支持 有限的 本国的
Python关键字 data={} json={}
Node.js URLSearchParams JSON.stringify()
可读性 简单的平面参数 更适合复杂数据
兼容性 随处可用 随处可用

响应格式

添加 json=1 以获取 JSON 响应,无论请求格式如何:

# Without json=1 — plain text response
resp = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
})
# Response: "OK|12345678"

# With json=1 — JSON response
resp = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
    "json": 1,
})
# Response: {"status": 1, "request": "12345678"}

始终使用 json=1 以便于解析。


Python 示例

形式编码

import requests

# Submit
resp = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
    "json": 1,
})
task_id = resp.json()["request"]

# Poll (always GET with query params)
resp = requests.get("https://ocr.captchaai.com/res.php", params={
    "key": "YOUR_API_KEY",
    "action": "get",
    "id": task_id,
    "json": 1,
})

JSON 正文

import requests

# Submit with JSON
resp = requests.post("https://ocr.captchaai.com/in.php", json={
    "key": "YOUR_API_KEY",
    "method": "userrecaptcha",
    "googlekey": "SITE_KEY",
    "pageurl": "https://example.com",
    "json": 1,
})
task_id = resp.json()["request"]

# Poll (same as form-encoded — GET with params)
resp = requests.get("https://ocr.captchaai.com/res.php", params={
    "key": "YOUR_API_KEY",
    "action": "get",
    "id": task_id,
    "json": 1,
})

Node.js 示例

形式编码

const axios = require('axios');
const qs = require('querystring');

// Submit
const resp = await axios.post(
  'https://ocr.captchaai.com/in.php',
  qs.stringify({
    key: 'YOUR_API_KEY',
    method: 'userrecaptcha',
    googlekey: 'SITE_KEY',
    pageurl: 'https://example.com',
    json: 1,
  })
);
const taskId = resp.data.request;

JSON 正文

const axios = require('axios');

// Submit with JSON
const resp = await axios.post(
  'https://ocr.captchaai.com/in.php',
  {
    key: 'YOUR_API_KEY',
    method: 'userrecaptcha',
    googlekey: 'SITE_KEY',
    pageurl: 'https://example.com',
    json: 1,
  }
);
const taskId = resp.data.request;

图像验证码:表单与 JSON

对于图像验证码,格式更重要:

带文件上传的表单(分段)

# File upload — form-encoded with multipart
resp = requests.post("https://ocr.captchaai.com/in.php",
    data={
        "key": "YOUR_API_KEY",
        "method": "post",
        "json": 1,
    },
    files={
        "file": open("captcha.png", "rb"),
    },
)

带有 Base64 的 JSON

import base64

# Base64 in JSON body
with open("captcha.png", "rb") as f:
    body = base64.b64encode(f.read()).decode()

resp = requests.post("https://ocr.captchaai.com/in.php", json={
    "key": "YOUR_API_KEY",
    "method": "base64",
    "body": body,
    "json": 1,
})

使用 Base64 形成表格

# Base64 in form data
resp = requests.post("https://ocr.captchaai.com/in.php", data={
    "key": "YOUR_API_KEY",
    "method": "base64",
    "body": body,
    "json": 1,
})

何时使用哪个

设想 受到推崇的 为什么
简单的脚本 表单编码 更简单,更少的依赖
REST API 集成 JSON 匹配典型的 API 模式
文件上传 多部分形式 直接二进制上传
大型 Base64 图像 表单编码 更好地处理大负载
TypeScript/modern JS JSON 本机对象支持
遗留系统集成 表单编码 通用兼容性
从 2Captcha 迁移 表单编码 与 2Captcha 格式相同

常见错误

错误 问题 处理方式
使用 json={} 但数据中没有 json: 1 响应是纯文本 数据中包含 "json": 1
在 Python 请求中混合 data=json= 请求格式错误 使用其中之一
忘记 Content-Type 标头 服务器无法解析正文 让你的 HTTP 库自动设置它
将 JSON 正文发送到轮询端点 轮询使用 GET 参数 始终将 GET 与 /res.php 的查询参数一起使用

常问问题

格式会影响求解速度或精度吗?

不会。两种格式都会产生相同的结果。服务器以相同的方式处理它们。

我可以在同一项目中混合使用格式吗?

是的。您可以使用 JSON 提交并使用查询参数进行轮询(这就是轮询始终工作的方式)。每个请求都是独立的。

2Captcha 兼容 API 使用哪种格式?

原始的 2Captcha API 使用表单编码。 CaptchaAI 在顶部添加了 JSON 支持。如果您要从 2Captcha 迁移,请坚持使用表单编码。


相关指南


选择您喜欢的格式 —尝试 CaptchaAI API今天。

该文章已禁用评论。