3.3 Golang Examples
Go code examples for interacting with the JSONAir API.
Authentication — Get a Bearer Token
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type tokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
}
func getToken(baseURL, pat string) (string, error) {
body, err := json.Marshal(map[string]string{"token": pat})
if err != nil {
return "", err
}
resp, err := http.Post(
baseURL+"/api/v1/jsonair/auth/token",
"application/json",
bytes.NewReader(body),
)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("auth failed: HTTP %d", resp.StatusCode)
}
var tr tokenResponse
if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
return "", err
}
return tr.AccessToken, nil
}Fetch Configuration Data
Putting It Together — Poll with Re-Authentication
Fetch the Reload Key
Last updated