Feature request: /tool fetch HTTP-POST can send a file

They added other things to scripting that make this, slightly, easier “manually”. No direct “multipart/form-data files” AFAIK in /tool/fetch however.

For example, with some of the new scripting this, you MIGHT, depending on file size/type, using v7.13+ be able to do this in script. But you can see that’s it’s not just "files="that multipart/form-data needs. There is the file’s MIME type, field name from “form”, and the desired filename.

I’ll provide some sample code, but don’t vouch it will work & there are still limits of scripting that still may NOT have a workaround in scripting. Mikrotik added a “:convert to=base64” operation which is what’s needed, in some case, to encode the file data.

:global sendFormDataFile do={
    :local urlf $url
    :local formFieldName $field
    :local fileName $file
    :local mimeSeperator "--$[:rndstr]" 
    :local contentType "Content-Type: multipart/form-data; boundary=$mimeSeperator"
    :local fileMimeType "application/octet-stream"

    :local formdataMime "\
    $mimeSeperator\n\
    Content-Disposition: form-data; name=\"$formFieldName\"; filename=\"$formName\"\n\
    Content-Type: $fileMimeType\n\
    Content-Transfer-Encoding: base64\n\n\
    $[:convert from=raw to=base64 [/file/get $fileName content]]\n\n\
    $mimeSeperator--\n"

    /tool/fetch http-method=post url=$urlf http-header-field=$contentType http-data=$formdataMime output=user
}

$sendFormDataFile url="http://192.168.88.1:80/upload" file=myrouterfile field=formfieldname

More for ideas, than suggesting this is a solution. form-data is tricky, since the other end needs to accept it and it may be looking for some very specific things to accept it. e.g. form-data is really a browser thing, so it kinda depends on the original HTML like what’s contained on the etc tags, on top of just the file data.

But if you can look at the HTTP transfer in a browser or sniffer, you’d have a better idea of how to build it RouterOS script. The above gives example of how to deal with the MIME headers which are critical for form-data. But what field name, encoding etc. etc. is needed depends on the server-end. It may need other form-data to in ANOTHER MIME block to even accept the file, too! But a sniffer or browser inspect of the HTTP headers/data from a browser give you a BIG clue how to build the http-data= needed in /tool/fetch.

Need send to

https://api.telegram.org/bot5955340922:AAE4Ljtk4Gm8anV8QoXhgck11CdNLCUaz1M/sendDocument\?chat_id=340234876

RB4011-backup–10202-0.backup backup file from router

https://core.telegram.org/bots/api#inputfile telegram bot api
Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files.

Ah, Telegram docs suggest this:

In sendDocument, sending by URL will currently only work for GIF, PDF and ZIP files.

And backup files are normally bigger than 64kB (and even bigger when base64-encoded for form-data).
So not going to work for backups I think…

But using /tool/e-mail is actually easiest/most reliable way to get files out of RouterOS. Unfortunately, Telegram has not appear to have some built-in email gateway to Telegram.

Perhaps, if it’s a new ARM Mikrotik, you can add a container that has curl. That let you do this since it supports the @filename syntax – but that’s a bit of lot of work.

Why you’re generally stuck with /tool/e-mail to get files off the box from a scheduled script.

I send the backup files directly to my FTP server located on my NAS.

e.g.

/tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$backupfilename dst-path=$backupfilename upload=yes port=$port;

The data is declared in local variables at the beginning of the script.

By Telegram I have not succeeded so far.