aREST
aREST copied to clipboard
Function gets called twice
The function gets called twice even if I execute the URL only once.. What could be the problem ?
Following is my code:
rest.function("status",espstatus);
.
.
.
.
int espstatus(String command){
Serial.print("Command recd: ");
Serial.println(command);
return 1;
}
This is how i call the url from browser:
http://esp-ip-address/status?params=testf123
And following is the output on my Serial monitor:
Command recd: testf123
Command recd: testf123
Hello,
This might happen when using aREST in your browser, as browsers usually send several requests to the server. If you are using it from a web app however this won't happen. Is this an issue for you?
Yes i am accessing the URL from web browser. Did not tried from app yet, will try and let you know the feedback.
Is there any update on this issue ? I think it's still quite a problem for a Rest (;-)) like API not to be called properly from browser. Solution, skip OPTIONS
request in the source code maybe ?
Still working on it - I found the source of the problem but it's hard to fix without breaking the compatibility with older boards like the Arduino Uno.
Found out that in chrome it come twice, but in edge only once. First time when I liked edge for a short time :)
Hello @marcoschwartz, maybe you could create a branch for a working version with earlier versions ? Really impressed by the aRest library btw 👍
I could "solve" the issue of duplicate function calls in Chrome (when directly calling using GET request from the browser URL field) by registering a second function for requests to "favicon.ico":
rest.function("favicon.ico", favIcon);
Just create a handler function that returns just anything for this call.
Once I set this up I do not get duplicate calls anymore to my other rest endpoints.
Maybe this helps one or the other.
Obviously this does not help for OPTIONS requests.
Haha, excellent! Didn't think about the favicon chrome call... Thank you for this really useful hint 👍
I created a fork that handles OPTIONS calls by directly sending a hopefully proper OPTIONS response (RC = 204, correct headers and no content). Use at your own risk :-) https://github.com/pkerspe/aREST
I changed the aRest.h file only, basically added a function to set the OPTIONS response headers in line 309 (please note: I followed the approach of the author to use : Access-Control-Allow-Origin: *
even though this is not a good practice in regards to security!)
void send_options_http_headers(){
addToBuffer(F("HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nAllow: POST, GET, OPTIONS\r\nAccess-Control-Allow-Methods: POST, GET, OPTIONS\r\nAccess-Control-Max-Age: 86400\r\n\r\n"));
}
and a check for OPTIONS request type in line 864:
if (answer.startsWith("OPTIONS")){
state = 'x'; //not sure about this state parameter, I just chose a value that seemed ok
command = 'o'; //this is a new command type, that has not been used before
return;
}
at at last a check for the new command type 'o' (which I added) in line 1096:
if (command == 'o'){ //checking for the new command type here
resetBuffer(); //clear output buffer, might not be needed, just wanted to be sure nothing is sent except for the headers
send_options_http_headers(); //send proper preflight-request response headers
return true;
}
That's basically it. Since I only use direct http rest api calls I did not test if it has any side effects on the rest to the module (pubsub calls etc.), unfortunately the inline doc is not very good, so I had to guess a bit if what I was doing was ok :-)
@pkerspe Thanks for your work! Indeed I know it's a nasty bug, basically I am mostly using it with direct REST calls from apps in Node.js so the favicon issue doesn't appear :)
Could you actually submit a pull request with your changes? That would be awesome!
@marcoschwartz The function still gets called twice when using aREST in the browser, is there a solution for this problem? is code that @pkerspe made, merged?
@marcoschwartz Actually rest.function("favicon.ico", favIcon); works fine for me, sry :)