Last active
January 4, 2025 20:20
-
-
Save mbentley/03c198077c81d52cb029b825e9a6dc18 to your computer and use it in GitHub Desktop.
Example API Calls Using Powershell and Bash/curl for Omada Controller (last validated on 5.12.7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### PowerShell Example | |
# set variables | |
$OMADA_URL = "https://omada.example.com:8043" | |
$USERNAME = "admin" | |
$PASSWORD = "test12345" | |
# get controller id from the API | |
$CONTROLLER_ID = (Invoke-RestMethod -Uri "${OMADA_URL}/api/info" -Method Get -UseBasicParsing).result.omadacId | |
# set the login request body as json | |
$loginRequestBody = @{ | |
username = $USERNAME | |
password = $PASSWORD | |
} | ConvertTo-Json | |
# login, get token, set a session variable | |
$loginResponse = Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -Method Post -ContentType "application/json" -Body $loginRequestBody -SessionVariable OmadaSession | |
# extract the token and create a variable for the headers | |
$TOKEN = $loginResponse.result.token | |
$RequestHeaders = @{ | |
"Csrf-Token" = $TOKEN | |
"Content-Type" = "application/json" | |
} | |
# validate login | |
Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}" -Method Get -Headers $RequestHeaders -WebSession $OmadaSession | |
# example to get info on the current user | |
Invoke-RestMethod -Uri "${OMADA_URL}/${CONTROLLER_ID}/api/v2/users/current?token=${TOKEN}¤tPage=1¤tPageSize=1000" -Method Get -Headers $RequestHeaders -WebSession $OmadaSession |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Bash Example | |
# set variables | |
OMADA_URL="https://omada.example.com:8043" | |
USERNAME="admin" | |
PASSWORD="test12345" | |
# get controller id from the API | |
CONTROLLER_ID="$(curl -sk "${OMADA_URL}/api/info" | jq -r .result.omadacId)" | |
# login, get token, set & use cookies | |
TOKEN="$(curl -sk -X POST -c "/tmp/omada-cookies.txt" -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/login" -d '{"username": "'"${USERNAME}"'", "password": "'"${PASSWORD}"'"}' | jq -r .result.token)" | |
# once logged in, make sure you add the following header on additional API calls: | |
# -H "Csrf-Token: ${TOKEN}" | |
# validate login | |
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/loginStatus?token=${TOKEN}" | jq . | |
# example to get info on the current user | |
curl -sk -X GET -b "/tmp/omada-cookies.txt" -H "Content-Type: application/json" -H "Csrf-Token: ${TOKEN}" "${OMADA_URL}/${CONTROLLER_ID}/api/v2/users/current?token=${TOKEN}¤tPage=1¤tPageSize=1000" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the example @mbentley. It works smoothly