أدلة التكامل
أمثلة تكامل الخادم الخلفي
التكامل على جانب الخادم مجرّد POST بـ JSON، فلا SDK طرف أول بأي لغة: استخدم أيّ عميل HTTP لدى خادمك الخلفي أصلًا. كل مثال أدناه يرسل سرّك والرمز من النموذج، ثم يقرأ success من الاستجابة. تفترض كلها أن السرّ في متغيّر بيئة، لا في الكود أبدًا.
Node (fetch، مدمج منذ 18)
const res = await fetch("https://caputchin.com/api/v1/siteverify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ secret: process.env.CAPUTCHIN_SECRET, response: token }),
});
const result = await res.json();
if (!result.success) throw new Error("verification failed");Python (requests)
import os, requests
res = requests.post(
"https://caputchin.com/api/v1/siteverify",
json={"secret": os.environ["CAPUTCHIN_SECRET"], "response": token},
timeout=10,
)
res.raise_for_status()
if not res.json()["success"]:
raise RuntimeError("verification failed")PHP (cURL)
$ch = curl_init("https://caputchin.com/api/v1/siteverify");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode([
"secret" => getenv("CAPUTCHIN_SECRET"),
"response" => $token,
]),
]);
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!$result["success"]) throw new Exception("verification failed");Go (net/http)
body, _ := json.Marshal(map[string]string{
"secret": os.Getenv("CAPUTCHIN_SECRET"),
"response": token,
})
res, err := http.Post("https://caputchin.com/api/v1/siteverify", "application/json", bytes.NewReader(body))
if err != nil {
return err
}
defer res.Body.Close()
var result struct {
Success bool `json:"success"`
}
json.NewDecoder(res.Body).Decode(&result)
if !result.Success {
return fmt.Errorf("verification failed")
}Ruby (Net::HTTP)
require "net/http"
require "json"
uri = URI("https://caputchin.com/api/v1/siteverify")
res = Net::HTTP.post(
uri,
{ secret: ENV["CAPUTCHIN_SECRET"], response: token }.to_json,
"Content-Type" => "application/json",
)
raise "verification failed" unless JSON.parse(res.body)["success"].NET (HttpClient)
using var http = new HttpClient();
var payload = JsonSerializer.Serialize(new {
secret = Environment.GetEnvironmentVariable("CAPUTCHIN_SECRET"),
response = token,
});
var res = await http.PostAsync(
"https://caputchin.com/api/v1/siteverify",
new StringContent(payload, Encoding.UTF8, "application/json"));
var result = JsonSerializer.Deserialize<JsonElement>(await res.Content.ReadAsStringAsync());
if (!result.GetProperty("success").GetBoolean()) throw new Exception("verification failed");curl (لاختبار سريع)
curl -sS https://caputchin.com/api/v1/siteverify \
-H "Content-Type: application/json" \
-d "{\"secret\":\"$CAPUTCHIN_SECRET\",\"response\":\"$TOKEN\"}"عميل مكتوب الأنواع
إن آثرت العمل بعميل مكتوب الأنواع، فوجّه مولّد OpenAPI إلى مرجع API التشغيل؛ يقيم /siteverify في مواصفة التشغيل، لا مواصفة الإدارة. العقد صغير، فالمخرَج المولَّد قصير.
انظر أيضًا
- التكامل على جانب الخادم لشكل الاستجابة والقواعد.