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" |
I'm running your docker container on an RPi4. Using node-red http request node, I can get the omadacId' with the first request OK But when trying to login I just get this error - "status":500,"error":"Internal Server Error","path":"/xxx/api/v2/login" This is what I send
url: "https://192.168.0.254:8043/7761d21f15884e11039b39a6daa1ee22/api/v2/login" headers: object Content-Type: "application/json" username: "user" password: "pass" method: "post"
Any ideas what I'm doing wrong ?
I would have to guess it is because you're not sending the data payload correctly. It needs to be JSON like: {"username": "admin", "password": "Testing12345!"}
.
Thanks for replying, I managed to sort this now.
Thank you for the example @mbentley. It works smoothly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm running your docker container on an RPi4.
Using node-red http request node, I can get the omadacId' with the first request OK
But when trying to login I just get this error - "status":500,"error":"Internal Server Error","path":"/xxx/api/v2/login"
This is what I send
url: "https://192.168.0.254:8043/7761d21f15884e11039b39a6daa1ee22/api/v2/login" headers: object Content-Type: "application/json" username: "user" password: "pass" method: "post"
Any ideas what I'm doing wrong ?