Community discussions

MikroTik App
 
Habbiton
just joined
Topic Author
Posts: 5
Joined: Fri Sep 17, 2021 2:46 pm

Migrate console commands to POST API REST

Tue Oct 05, 2021 2:52 pm

Hi there!

I'm trying to migrate from console commands to REST API. As I see on the docs (https://help.mikrotik.com/docs/display/ROS/REST+API), I should use the POST methods
However, I can't make some commands work, like adding some comments to an interface (which could be done using commands with " /interface/comment ether1 "WAN" ").
Should I place something on headers? If so, what? And with what format?

Note: I've been able to do it using PATCH method and editing that interface, but finding an easy way to translate console commands to POST API REST would be amazing

Thanks in advise
 
Habbiton
just joined
Topic Author
Posts: 5
Joined: Fri Sep 17, 2021 2:46 pm

Re: Migrate console commands to POST API REST

Thu Oct 07, 2021 2:01 pm

I found another example of a command that I don't know how to send through API REST:
"/interface/enable wlan1"
 
Habbiton
just joined
Topic Author
Posts: 5
Joined: Fri Sep 17, 2021 2:46 pm

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 9:04 am

Is there no one who uses commands like "/command applyToThis" or "/command applyToThis withThisValue" and can help me?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6345
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 9:26 am

Get the ID of an item you want to make actions with.

For example, get ether1 ID
-X POST https://x.x.x.x/rest/interface/print \
  --data '{".proplist": [".id"], ".query": ["name:"ether1"]}'\
  -H "content-type: application/json"
Lets say it returned id=*1

Now you can use this id to set values or run commands on it
-X POST https://x.x.x.x/rest/interface/enable \
  --data '{".id":"*1"}' -H "content-type: application/json"
-X POST https://x.x.x.x/rest/interface/comment \
  --data '{".id":"*1","comment":"my comment"}' -H "content-type: application/json"
to set param values now you can use either patch or post:
-X PATCH https://x.x.x.x/rest/interface/*1 \
  --data '{"comment": "my other comment"}' -H "content-type: application/json"
 
sid5632
Member
Member
Posts: 475
Joined: Fri Feb 17, 2017 6:05 pm

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 1:12 pm

["name="ether1"]
That doesn't look right...
 
Habbiton
just joined
Topic Author
Posts: 5
Joined: Fri Sep 17, 2021 2:46 pm

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 4:38 pm

Get the ID of an item you want to make actions with.

For example, get ether1 ID
-X POST https://x.x.x.x/rest/interface/print \
  --data '{".proplist": [".id"], ".query": ["name:"ether1"]}'\
  -H "content-type: application/json"
Lets say it returned id=*1

Now you can use this id to set values or run commands on it
-X POST https://x.x.x.x/rest/interface/enable \
  --data '{".id":"*1"}' -H "content-type: application/json"
-X POST https://x.x.x.x/rest/interface/comment \
  --data '{".id":"*1","comment":"my comment"}' -H "content-type: application/json"
to set param values now you can use either patch or post:
-X PATCH https://x.x.x.x/rest/interface/*1 \
  --data '{"comment": "my other comment"}' -H "content-type: application/json"
Hi mrz, thanks for the help. The above method works!
One more question, is not possible to do those type of POST request without knowing the ID, right? I mean, you can`t make something like this:
 https://x.x.x.x/rest/interface/enable \
  --data '{".query": ["name:"ether1"]}' 
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6345
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 5:43 pm

No you cannot do that with REST API.
REST API working principle is similar to API
https://help.mikrotik.com/docs/display/ROS/API
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 842
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 5:57 pm

ROS, API and REST do support names as primary identifiers for master data:
# ROS
[admin@MikroTik] > /interface enable ether1

# API
/interface/enable
=.id=ether1

# REST
curl -k -u api:api -X POST \
    https://192.168.88.1/rest/interface/enable \
    --data '{".id":"ether1"}' \
    -H "content-type: application/json"
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6345
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 6:22 pm

I wouldn't rely on that. When using names instead of IDs you may get unexpected results.
 
nescafe2002
Forum Veteran
Forum Veteran
Posts: 842
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Migrate console commands to POST API REST

Mon Oct 11, 2021 6:30 pm

What unexpected results? Are there side-effects other than the obvious one, naming the interface *num?
[admin@MikroTik] /interface> set ether1 name=*A
[admin@MikroTik] /interface> disable *A
no such item (4)

Who is online

Users browsing this forum: No registered users and 6 guests