fetch post to azure function results in bad request

Product: MikroTik Knot
RouterOS: 6.48.3

Hello everybody,

I have a strange problem using fetch in a script and want to ask you for your help!
I’m working on something similar to this tutorial by MikroTik: https://help.mikrotik.com/docs/pages/viewpage.action?pageId=41680915#heading-HTTPfetchwithscriptscenario

I want to post the resulting data to an Azure Function for further processing.
And there is the problem: the post using fetch results in a bad request.
And I have no ideo why.

To make sure there isn’t a problem with my Azure Function I tried posting some dummy data via Postman, Curl and PowerShell - without any issues.
Also, there is no problem when I use another service like https://webhook.site/ (instead of my Azure Function) to post the data to via fetch from script or terminal.

So, I guess, there might be some problem with the URI pointing to my Azure Function!?
The URI looks like this: https://function-name.azurewebsites.net/api/ActionName?code=********************************************************

I know I have to escape the ‘?’ character but it doesn’t change the result.

As far as I can tell, the Azure Function itself doesn’t even get called (at least there are no logs that would indicate someone tried to call the function).

Can anyone help me with this problem?

No, you do not have to escape the ? on script, unless you try on terminal.

Peovide the script and provide the complete full url anonymizing the character with * on url but without delete or replace any non-alpa like . , ! , ? , - , = , & , etc.

Hello rextended,

thank you for your quick reply!

About escaping the ‘?’ char:
Thank you for clarification about this one.

About the script (including the URI with anonymized function name and anonymized non-alphanumerical characters for code paramter):

# Required packages: iot

################################## Bluetooth ##################################
:local advertisements [/iot bluetooth scanners advertisements print detail as-value where true]
/iot bluetooth scanners advertisements clear
:local advJson ""
:local advSeparator ""

:foreach adv in=$advertisements do={
    :local address ($adv->"address")
    :local epoch ($adv->"epoch")
    :local rssi ($adv->"rssi")
    :local obj "\
        {\
            \"address\":\"$address\",\
            \"epoch\":$epoch,\
            \"rssi\":$rssi\
        }"
    # Ensure that the last object is not terminated by a comma
    :set $advJson "$advJson$advSeparator$obj"
    :if ($advSeparator = "") do={
        :set $advSeparator ","
    }
}

#################################### HTTP #####################################
:local message "[$advJson]"

# test endpoint
#:local url "https://webhook.site/********-****-****-****-************"

# azure function endpoint
:local url "https://function-name.azurewebsites.net/api/ActionName?code=******/***********************************************=="

:put ("[*] Total message size: $[:len $message] bytes")
:do {
    /tool fetch http-method=post http-header-field="Content-Type: application/json" http-data=$message url=$url
    :put ("[*] Done")
} on-error={
    :put ("[*] Failed")
};

I do not have that device and I can not test the script, but all appear ok,

the space here must be removed and be all lowercase:
from
http-header-field=“Content-Type: application/json”
to
http-header-field=“content-type:application/json”


replace in the url / with %2F and = with %3D, but not the = after code!!!

from this

https://function-name.azurewebsites.net/api/ActionName?code=******/***********************************************==

to this

https://function-name.azurewebsites.net/api/ActionName?code=******%2F***********************************************%3D%3D

(only on terminal put \ before?)

if spaces are present on “code” must used the + not %20

Good morning rextended!

Thank you very much for your help!

I changed “Content-Type: application/json” to “content-type:application/json” and it just works!
So, there wasn’t any problem with the URI after all.

Still, i wonder why the content-type was working for webhook.site but not for azure …

probably because webhook do not check that field…
the space must not be present, or it come part of the value…

And about the url, azure can probably ignore if the value come “urlencoded” or not