API: set `contents` of file

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>"');

is a standard HTML file.
I’m receiving no such command prefix as return…

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

Thx!

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.

/file/set
=.id=test
=contents=sdfg

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.

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 :wink:. Of course I prefer the new lines to be in the code - but at that moment I’m not sure how to solve it.

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.

Thanks :wink:.
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 :slight_smile:.

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

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

basically this: <0x00>
or: <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 == you can place whatever bytes you like while they are accepted by the argument.

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 :wink:):

/file print file=x
/file set x.txt contents=fuh

I don’t want my files to be txt… :wink:

Anyone? :slight_smile:

Now, you’re asking for too much :wink: Linux powered devices can’t rename files :slight_smile:

Is there a mv implemented? Did I miss something? :slight_smile:

dominikh,

There is no mv or rename file ability on RouterOS.

Well, that’s an answer, thanks! :slight_smile: