Saltar a contenido

Ejemplos de Go / Golang – API de Salud y Fitness

Acceda a la API de Salud y Fitness en sus aplicaciones Go utilizando el paquete estándar net/http. Se admiten tanto las solicitudes GET como POST.


1. Ejemplo de Solicitud GET

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    apiKey := "YOUR_API_KEY"
    url := "https://api.hefitapi.com/api/v1/bmi?height=178&weight=82&units=metric&lang=en"

    client := &http.Client{}
    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        panic(err)
    }

    req.Header.Add("X-API-Key", apiKey)

    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

2. Ejemplo de Solicitud POST

package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    apiKey := "YOUR_API_KEY"
    url := "https://api.hefitapi.com/api/v1/bmi/post"
    payload := []byte(`{"height":178,"weight":82,"units":"metric"}`)

    client := &http.Client{}
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
    if err != nil {
        panic(err)
    }

    req.Header.Add("X-API-Key", apiKey)
    req.Header.Add("Content-Type", "application/json")

    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

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 Go hoy mismo!


API de Salud y Fitness Impulsando la infraestructura digital de salud moderna