Community discussions

MikroTik App
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

API: set `contents` of file

Tue Jul 05, 2011 7:55 pm

RouterOS: 4.6

Hi there!

I'm currently trying to set HTML content to a file on my Mikrotik. But actually it is not working. I'm able to set simple content but it is not possible to set HTML as content.
I'm executing it like that
$conn->write('/file/set', false);
$conn->write('=numbers=hotspot/login.html', false);
$conn->write('=contents="<HTML-Content>"');
<HTML-Content> is a standard HTML file.
I'm receiving no such command prefix as return...

Is there a way to set HTML via API? :)

Thx!
 
reverged
Member Candidate
Member Candidate
Posts: 270
Joined: Thu Nov 12, 2009 8:30 am

Re: API: set `contents` of file

Tue Jul 05, 2011 10:32 pm

Can you post the html?

API uses newlines as a delimiter, so at a minimum, you will need to strip newlines from the html.

There may be other common html characters that need escaping as well.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: API: set `contents` of file

Thu Jul 07, 2011 1:25 pm

/file/set
=.id=test
=contents=<html><body>sdfg</body></html>
this works.

However using my C/C++ implementation, i can do this:
/file/set
=.id=test
=contents=<html>\n<body>\nadfa\n</body>\n</html>
and result is
<html>                                                                                                                               
<body>                                                                                                                               
adfa                                                                                                                                 
</body>                                                                                                                              
</html>

so, the key is to give your html file and correct length of the argument.
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Thu Jul 07, 2011 8:25 pm

Thanks for your replies!

Well - actually I'm reading from a file.
In my case I'm reading a file with that content (for test):
<html>
<head>
<title>Login</title>
</head>

<body>
<div id="test">test</div>
</body>
</html>
I were not able to transfer that to the box.
So I str_replace()d \n with nothing... So I loose my formatting of the code (which is not good - but I can live with it if necessary) but I'm able to transfer it ;-). Of course I prefer the new lines to be in the code - but at that moment I'm not sure how to solve it.
 
reverged
Member Candidate
Member Candidate
Posts: 270
Joined: Thu Nov 12, 2009 8:30 am

Re: API: set `contents` of file

Thu Jul 07, 2011 9:14 pm

Can you post the html?

API uses newlines as a delimiter, so at a minimum, you will need to strip newlines from the html.

There may be other common html characters that need escaping as well.
I should have written: "My version of the php api class uses newlines as a delimiter....I changed that."

I then tried this:

test.html
<html>
<head>
<title>Login</title>
</head>

<body>
<div id="test">test</div>
</body>
</html>
test.php
<?php

require('routeros_api.class.php');

$API = new routeros_api();

$API->debug = true;

if ($API->connect('<ip>','admin','<password>')) {
$text = file_get_contents('test.html');
echo $text;

   $API->write('/file/set',false);
   $API->write('=.id=test.txt',false);
   $API->write("=contents=" . $text);
   $READ = $API->read(false);
   $ARRAY = $API->parse_response($READ);

   print_r($ARRAY);

   $API->disconnect();

}

?>
On RB, after running test.php, test.txt looks like:
<html>
<head>
<title>Login</title>
</head>

<body>
<div id="test">test</div>
</body>
</html>
Works perfectly.
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Fri Jul 08, 2011 9:49 pm

Thanks ;-).
I did not walk through the code... It would have been better if I did that.
Just the question what's an ideal delimiter instead of \n - because there are many chars that could be in a HTML... hm :).
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: API: set `contents` of file

Mon Jul 11, 2011 2:44 pm

there is no need additional delimiter in API as <len> is there for this purpose

what you do is you send correctly encoded length of sting you are going to send <len> and then string itelf <string>.

basically this: <len><string><0x00>
or: <len><string><len><string><0x00>

RouterOS will read the length and then expect that number of bytes to follow. In that string RouterOS will look for command name (/ip/address/print) or attribute (=comment=yadayada) where after =<attibute>= you can place whatever bytes you like while they are accepted by the argument.
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Wed Jul 13, 2011 10:13 am

Thanks =)!
I'm fine with that now.

Just the question - how to create a new file via api...?

Is there another way than (in "scripting" language ;-)):
/file print file=x
/file set x.txt contents=fuh
I don't want my files to be txt... ;-)
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Tue Jul 19, 2011 10:30 pm

Anyone? :-)
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: API: set `contents` of file

Wed Jul 20, 2011 9:18 pm

I don't want my files to be txt... ;-)
Now, you're asking for too much ;) Linux powered devices can't rename files :)
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Sun Jul 24, 2011 10:56 pm

Is there a mv implemented? Did I miss something? :-)
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: API: set `contents` of file

Mon Jul 25, 2011 2:00 am

dominikh,

There is no mv or rename file ability on RouterOS.
 
dominikh
just joined
Topic Author
Posts: 19
Joined: Mon Mar 17, 2008 11:48 am
Location: Austria

Re: API: set `contents` of file

Tue Jul 26, 2011 11:03 pm

Well, that's an answer, thanks! :-)

Who is online

Users browsing this forum: No registered users and 26 guests