コンテンツにスキップ

JavaScript の例 – ヘルスフィットネス API

ブラウザまたは Node.js で、現代の JavaScript を使用して ヘルスフィットネス API とのやり取りが可能です。以下に、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 を実際の API キーに置き換えてください。
  • lang パラメータは、複数の言語での応答をサポートします (en, fr, de, es など)。
  • 異常や例外を適切に処理してください (特に非同期操作の場合)。
  • 現代的なフロントエンドアプリには axios または fetch を使用し、レガシーサポートには XMLHttpRequest を使用します。
  • API レスポンスには、リスク信号、アクションプラン、および _enterprise などのメタデータを含む完全な JSON オブジェクトが含まれます (利用可能な場合)。

今日からヘルスフィットネス API を Python アプリケーションに統合しましょう!


ヘルスフィットネス API 現代のデジタルヘルスインフラを駆動