जावास्क्रिप्ट उदाहरण – हेल्थ फिटनेस एपीआई¶
आप आधुनिक जावास्क्रिप्ट का उपयोग करके ब्राउज़रों या Node.js में हेल्थ फिटनेस एपीआई के साथ इंटरैक्ट कर सकते हैं। नीचे GET और POST अनुरोधों को कवर करने वाले कई तरीके दिए गए हैं।
1. fetch (आधुनिक जावास्क्रिप्ट) का उपयोग करना¶
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 सिंटैक्स (आधुनिक जावास्क्रिप्ट)¶
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 अनुप्रयोगों में हेल्थ फिटनेस एपीआई को एकीकृत करना शुरू करें!
हेल्थ फिटनेस एपीआई आधुनिक डिजिटल स्वास्थ्य बुनियादी ढांचे को सशक्त बनाना