أمثلة 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:
مثال لطلب 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عند توفرها.
- PHP
- Python
- NodeJs
- Kotlin / Android
- Flutter / Dart
- React Native / Expo
- C# / .NET
- Go / Golang
- Ruby / Rails
ابدأ في دمج واجهة برمجة تطبيقات الصحة واللياقة البدنية في تطبيقات Python الخاصة بك اليوم!
واجهة برمجة تطبيقات الصحة واللياقة البدنية توفير البنية التحتية الرقمية الصحية الحديثة