Community discussions

MikroTik App
 
apilhar
just joined
Topic Author
Posts: 3
Joined: Thu Aug 05, 2021 12:26 pm

fetch post to azure function results in bad request

Thu Aug 05, 2021 1:35 pm

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/vi ... ptscenario

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 ... Name?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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch post to azure function results in bad request

Thu Aug 05, 2021 2:35 pm

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.
 
apilhar
just joined
Topic Author
Posts: 3
Joined: Thu Aug 05, 2021 12:26 pm

Re: fetch post to azure function results in bad request

Thu Aug 05, 2021 5:07 pm

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")
};
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch post to azure function results in bad request  [SOLVED]

Thu Aug 05, 2021 5:34 pm

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
 
apilhar
just joined
Topic Author
Posts: 3
Joined: Thu Aug 05, 2021 12:26 pm

Re: fetch post to azure function results in bad request

Fri Aug 06, 2021 9:26 am

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 ...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: fetch post to azure function results in bad request

Fri Aug 06, 2021 10:45 am

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

Who is online

Users browsing this forum: wirelesslywired and 10 guests