انتقل إلى المحتوى

أمثلة JavaScript – واجهة برمجة تطبيقات الصحة واللياقة البدنية

يمكنك التفاعل مع واجهة برمجة تطبيقات الصحة واللياقة البدنية باستخدام JavaScript الحديث في المتصفحات أو Node.js. فيما يلي عدة طرق تغطي طلبات GET و POST.


1. باستخدام fetch (JavaScript الحديث)

مثال لطلب GET:

const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

fetch(url, {
  headers: {
    "X-API-Key": apiKey
  }
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error("Error:", err));

مثال لطلب POST:

const postUrl = "https://api.hefitapi.com/api/v1/bmi/post";

const payload = {
  height: 178,
  weight: 82,
  units: "metric"
};

fetch(postUrl, {
  method: "POST",
  headers: {
    "X-API-Key": apiKey,
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(err => console.error("Error:", err));

2. باستخدام axios (المتصفح و Node.js)

التثبيت في Node.js:

npm install axios

مثال لطلب GET:

const axios = require("axios");

const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi";

axios.get(url, {
  headers: { "X-API-Key": apiKey },
  params: { height: 178, weight: 82, units: "metric", lang: "en" }
})
.then(response => console.log(response.data))
.catch(err => console.error("Error:", err));

مثال لطلب POST:

axios.post("https://api.hefitapi.com/api/v1/bmi/post", {
  height: 178,
  weight: 82,
  units: "metric"
}, {
  headers: { "X-API-Key": apiKey, "Content-Type": "application/json" }
})
.then(response => console.log(response.data))
.catch(err => console.error("Error:", err));

3. باستخدام XMLHttpRequest (طريقة المتصفح الكلاسيكية)

مثال لطلب GET:

const xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en");
xhr.setRequestHeader("X-API-Key", "YOUR_API_KEY");

xhr.onload = () => {
  if (xhr.status === 200) {
    console.log(JSON.parse(xhr.responseText));
  } else {
    console.error("Error:", xhr.statusText);
  }
};

xhr.send();

مثال لطلب POST:

const xhrPost = new XMLHttpRequest();
xhrPost.open("POST", "https://api.hefitapi.com/api/v1/bmi/post");
xhrPost.setRequestHeader("X-API-Key", "YOUR_API_KEY");
xhrPost.setRequestHeader("Content-Type", "application/json");

xhrPost.onload = () => {
  if (xhrPost.status === 200) {
    console.log(JSON.parse(xhrPost.responseText));
  } else {
    console.error("Error:", xhrPost.statusText);
  }
};

xhrPost.send(JSON.stringify({ height: 178, weight: 82, units: "metric" }));

4. بناء جملة Async/Await (JavaScript الحديث)

async function fetchBMI() {
  const apiKey = "YOUR_API_KEY";
  const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";

  try {
    const response = await fetch(url, {
      headers: { "X-API-Key": apiKey }
    });
    const data = await response.json();
    console.log(data);
  } catch (err) {
    console.error("Error:", err);
  }
}

fetchBMI();

5. ملاحظات وأفضل الممارسات

  • استبدل YOUR_API_KEY بمفتاح واجهة برمجة التطبيقات الخاص بك.
  • يدعم المعامل lang الاستجابات متعددة اللغات (مثل: en, fr, de, es, إلخ).
  • تعامل مع الأخطاء والاستثناءات، وخاصةً لعمليات غير متزامنة.
  • استخدم axios أو fetch لتطبيقات الواجهة الأمامية الحديثة؛ XMLHttpRequest بشكل أساسي لدعم الأنظمة القديمة.
  • تتضمن استجابات واجهة برمجة التطبيقات كائنات JSON كاملة مع إشارات المخاطر وخطط العمل ومعلومات _enterprise عند توفرها.

ابدأ في دمج واجهة برمجة تطبيقات الصحة واللياقة البدنية في تطبيقات Python الخاصة بك اليوم!


واجهة برمجة تطبيقات الصحة واللياقة البدنية توفير البنية التحتية الرقمية الصحية الحديثة