Community discussions

MikroTik App
 
sbotnick
newbie
Topic Author
Posts: 26
Joined: Fri Apr 21, 2017 10:54 pm

tool fetch xml

Fri Sep 06, 2024 6:37 am

Hello,

I am trying to control a device that the API outputs XML as a result of a GET. I don't care about the output, I just want to send a command with the API, but a required part of the API string tells it to output XML, so it always sends back a response. Easily ignored in curl.

So:
/tool fetch mode=http  url=http://192.168.1.134/state.xml?<some command string> 
status: failed
failure: ERROR parsing http: error in line
I've tried various permutations including by address and src-path, with output=user, as-value to no avail.

Am I doing it wrong or missing arguments or does fetch just not like getting back XML?

For reference, this is what it sends back with curl:
curl "http://192.168.1.134/state.xml?<some command string>"
<?xml version="1.0" encoding="utf-8" ?>
<datavalues>
...
In case it matters, I've tried it on ROS 7.15.1 and 7.15.3.
 
User avatar
patrikg
Member
Member
Posts: 362
Joined: Thu Feb 07, 2013 6:38 pm
Location: Stockholm, Sweden

Re: tool fetch xml

Fri Sep 06, 2024 11:48 am

I think you need to read little more about the api and the fetch command.
https://help.mikrotik.com/docs/display/ROS/Fetch

https://help.mikrotik.com/docs/display/ROS/REST+API

You need some more settings like http-method http-header-field http-data.
 
optio
Forum Veteran
Forum Veteran
Posts: 945
Joined: Mon Dec 26, 2022 2:57 pm

Re: tool fetch xml

Fri Sep 06, 2024 10:05 pm

@sbotnick This is not related to fetch command arguments, even mode is not necessary for http(s) and http-method argument is GET by default if not specified.
Try to fetch for eg. https://mocktarget.apigee.net/xml which also responds with XML and it will work -
/tool/fetch url=http://mocktarget.apigee.net/xml


Mentioned error is related to parsing HTTP protocol data from response, can be corrupted response when fetching from ROS (partial or no HTTP protocol data) or complete response data for some reason breaks fetch (unlikely).
You can use Packet Sniffer tool, while sniffer is running perform fetch from ROS and examine if HTTP response is valid.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4324
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: tool fetch xml

Fri Sep 06, 2024 10:18 pm

Since `curl` defaults to GET, and that works... my guess is some escaping issue in the URL - as OP show just "<some command string>" ...while the URL itself shouldn't require any escaping as shown... depending what in the ?query params part, that may need escaping.

So I'd try putting quotes around the url="http://...." and see what that gets you. If you have a $ or ", those need \$ or \" escapes (inside the url="..." outer quotes, to be clear).

Also, you may want to add an "output=user" to your /tool/fetch, to view the results at the CLI.
 
optio
Forum Veteran
Forum Veteran
Posts: 945
Joined: Mon Dec 26, 2022 2:57 pm

Re: tool fetch xml

Fri Sep 06, 2024 11:00 pm

If query arguments are not encoded correctly, HTTP server should respond with some HTTP error code if there is some wrong value and fetch failure message will be formatted depending on HTTP response code, eg. Fetch failed with status 400, but if HTTP service is not responding by standard and wrong values in url results respond without data or not HTTP protocol data then error mentioned in OP could happen.
Still I will suggest to examine HTTP request and response from sniffer (by examining packets it is visible how characters are encoded in request), also examining logs on HTTP service side if exists won't hurt...
 
sbotnick
newbie
Topic Author
Posts: 26
Joined: Fri Apr 21, 2017 10:54 pm

Re: tool fetch xml  [SOLVED]

Sat Sep 07, 2024 2:33 am

Thanks all for the input!

You gave me the idea to fetch the root and I got a 401 error! Somebody password protected it on me, that was not expected. The server put out the correct response on the root request but not on the state.xml request when authentication was not included. I added authentication to fetch command and it does the job. I should have checked to see that things were working as expected in other ways.

Beverages on me.

FWIW, this fetch is going into a watchdog script to make a PDU power cycle the upstream DSL modem when the router loses Internet connection.

Addendum: My older devices only speak HTTP/0.9 and newer versions of curl don't work at all and wget requires -O- to force it, I assume m'tik uses one of those and it won't work with them, unfortunately. Works fine with the newer series of device that speak 1.x.
Last edited by sbotnick on Tue Sep 10, 2024 6:42 am, edited 1 time in total.
 
User avatar
patrikg
Member
Member
Posts: 362
Joined: Thu Feb 07, 2013 6:38 pm
Location: Stockholm, Sweden

Re: tool fetch xml

Sat Sep 07, 2024 8:51 pm

Hi, I'm very glad you solved the problem, but I and the forum would be so much happier
if you also mark your case in this forum as solved.

See the pictures below.
Where you click on the following places on the thread that solved your problem.

So the AI can take over and know what the problems are with the respective solution.

At board style: Canvas
Image

At board style: prosilver
Image

Who is online

Users browsing this forum: Extrems, fredgr, tornadoro, vingjfg and 33 guests