Community discussions

MikroTik App
 
hats
just joined
Topic Author
Posts: 13
Joined: Sat Jun 23, 2018 9:52 pm

REST API - is it a bug? [SOLVED]

Tue Apr 09, 2024 12:51 am

Hello dear - i am trying to write simple ansible playbook, but i stuck.

For example i have one routerboard with ROS 7.12. When i execute call to REST API:

curl -k -u admin:admin -X GET http://192.168.0.1/rest/system/package/update | jq .

{
"channel": "stable",
"installed-version": "7.12"
}

If i log via winbox - /system/packages/check-for-updates and then execute same call to REST API - i see different result:

{
"channel": "stable",
"installed-version": "7.12",
"latest-version": "7.14.2",
"status": "New version is available"
}

Is it normal behavior? I have checked it on different boards with and CHR with stable ROS 7.9<--->7.14.2 - same problem.

Any ideas and suggetions?

P.S. just checked another board:

{
"channel": "stable",
"installed-version": "7.12",
"latest-version": "7.13.4",
"status": "New version is available"
}

Why not 7.14.2? I am confused.
Last edited by hats on Tue Apr 09, 2024 6:04 pm, edited 1 time in total.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3593
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: REST API - is it a bug?

Tue Apr 09, 2024 3:40 am

To check for updates with rest, use POST instead of GET:
USER='admin:admin' ROUTER=192.168.88.1 ; curl -k -u $USER -X POST https://$ROUTER/rest/system/package/update/check-for-updates --json ''
or to actually download & install:
USER='admin:admin' ROUTER=192.168.88.1 ; curl -k -u $USER -X POST https://$ROUTER/rest/system/package/update/install --json ''

Whether its a bug that a GET does NOT match the CLI... IDK. I suspect the CLI returns data it knows at time of the call. Since the new version available stuff requires a fetch, it's not that odd it should use a POST to cause the remote connection (vs GET which is more to show config).

Perhaps better stated the GET /system/package/update will use a cache'd value form the last check-for-update, if none you'll get nothing. If last time it check it was 7.13.x, that what it will show since it's cached. Why you're seeing variability in the versions. Basically you need to use the "check-for-updates" with POST.
 
hats
just joined
Topic Author
Posts: 13
Joined: Sat Jun 23, 2018 9:52 pm

Re: REST API - is it a bug?

Tue Apr 09, 2024 6:02 pm

Thank you very much - it works.
 
JDF
just joined
Posts: 12
Joined: Wed Aug 16, 2023 1:40 pm

Re: REST API - is it a bug? [SOLVED]

Wed Apr 10, 2024 1:33 pm

Hey

But what should the post data be? It gives me an error if its left empty but I didn't figure out the correct post data.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3593
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: REST API - is it a bug? [SOLVED]

Wed Apr 10, 2024 2:55 pm

But what should the post data be? It gives me an error if its left empty but I didn't figure out the correct post data.
I do not think it needs a parameters. But the content-type needs to be set to application/json. So the
--json ''
part does that. If using an older curl, it may not have the --json option. You can try:
-H 'Content-Type: application/json' -d ''

If that's not it, post the curl command you're using (with user:password and router IP obfuscated). Also if you're using Windows....the env vars shown is not going to work — those work in WSL or Linux/Mac.
 
JDF
just joined
Posts: 12
Joined: Wed Aug 16, 2023 1:40 pm

Re: REST API - is it a bug? [SOLVED]

Wed Apr 10, 2024 9:43 pm

My bad, it was a problem with the group policies. The user didn't have the correct rights so it gave me the
{
    "detail": "not enough permissions (9)",
    "error": 500,
    "message": "Internal Server Error"
}
But at first I didn't see the detail as I was using Comfortclick's http driver to test it and it only said Internal server error... I needed to add policy=local,reboot,read,write,policy,test,api,rest-api policies too, then it worked.

Thank you!
Last edited by JDF on Wed Apr 10, 2024 10:55 pm, edited 1 time in total.
 
infabo
Forum Veteran
Forum Veteran
Posts: 735
Joined: Thu Nov 12, 2020 12:07 pm

Re: REST API - is it a bug? [SOLVED]

Wed Apr 10, 2024 9:57 pm

HTTP 500 to say not enough permission. It hurts. It is hard to consume APIs that return crap status codes.

Better: HTTP 403 Forbidden

Maybe even another status could be more appropriate.

HTTP 418 for the REST API developer....
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3593
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: REST API - is it a bug? [SOLVED]

Thu Apr 11, 2024 6:40 am

Your right it's not a server error so 5xx status code is wrong.

Although the specific permission that's missing be more helpful.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3593
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: REST API - is it a bug? [SOLVED]

Thu Apr 11, 2024 6:45 am

But at first I didn't see the detail as I was using Comfortclick's http driver to test it and it only said Internal server error...
FWIW, if you use Postman to test request, I created a RAML/OpenAPI scheme that allows testing of the REST API. See viewtopic.php?t=199476&hilit=postman
 
JDF
just joined
Posts: 12
Joined: Wed Aug 16, 2023 1:40 pm

Re: REST API - is it a bug? [SOLVED]

Thu Apr 11, 2024 9:33 am

I poked it until it worked for me. I gave the API user all group rights and then the command worked - Then I removed the policies one by one. So at the end I left only policy=local,reboot,read,write,policy,test,api,rest-api active. All other commands i've added before worked with just group policy=read,write,test,api,rest-api.

I thank you again Amm0 for making the RAML/OpenAPI scheme, it has been useful.

Who is online

Users browsing this forum: No registered users and 7 guests