Ejemplos de React Native / Expo – API de Salud y Fitness¶
Acceda a la API de Salud y Fitness en sus aplicaciones de React Native / Expo utilizando fetch o axios. Ambos métodos funcionan sin problemas para las solicitudes GET y POST.
1. Utilizando fetch¶
Ejemplo de solicitud 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, {
method: "GET",
headers: {
"X-API-Key": apiKey
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
Ejemplo de solicitud POST:
const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi/post";
const payload = {
height: 178,
weight: 82,
units: "metric"
};
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey
},
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error("Error:", error));
2. Utilizando axios¶
Primero, instale axios:
Ejemplo de solicitud GET:
import axios from "axios";
const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en";
axios.get(url, { headers: { "X-API-Key": apiKey } })
.then(response => console.log(response.data))
.catch(error => console.error("Error:", error));
Ejemplo de solicitud POST:
import axios from "axios";
const apiKey = "YOUR_API_KEY";
const url = "https://api.hefitapi.com/api/v1/bmi/post";
const payload = { height: 178, weight: 82, units: "metric" };
axios.post(url, payload, { headers: { "X-API-Key": apiKey } })
.then(response => console.log(response.data))
.catch(error => console.error("Error:", error));
Otros Lenguajes¶
Explore cómo integrar la API de Salud y Fitness en otros lenguajes de programación:
¡Comience a integrar la API de Salud y Fitness en sus aplicaciones móviles y web hoy mismo!
API de Salud y Fitness Potenciando la infraestructura digital de salud moderna