aREST icon indicating copy to clipboard operation
aREST copied to clipboard

Function gets called twice

Open romuye123 opened this issue 7 years ago • 12 comments

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

romuye123 avatar Mar 07 '17 08:03 romuye123

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?

marcoschwartz avatar Mar 08 '17 12:03 marcoschwartz

Yes i am accessing the URL from web browser. Did not tried from app yet, will try and let you know the feedback.

romuye123 avatar Mar 09 '17 13:03 romuye123

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 ?

adadgio avatar Aug 22 '17 11:08 adadgio

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.

marcoschwartz avatar Aug 31 '17 09:08 marcoschwartz

Found out that in chrome it come twice, but in edge only once. First time when I liked edge for a short time :)

pta- avatar Nov 07 '17 23:11 pta-

Hello @marcoschwartz, maybe you could create a branch for a working version with earlier versions ? Really impressed by the aRest library btw 👍

JLartigaud avatar Jan 26 '18 00:01 JLartigaud

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.

pkerspe avatar Jan 29 '18 20:01 pkerspe

Haha, excellent! Didn't think about the favicon chrome call... Thank you for this really useful hint 👍

JLartigaud avatar Jan 29 '18 20:01 JLartigaud

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 avatar Jan 29 '18 22:01 pkerspe

@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 avatar Feb 11 '18 14:02 marcoschwartz

@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?

nenadvkc avatar Dec 29 '21 18:12 nenadvkc

@marcoschwartz Actually rest.function("favicon.ico", favIcon); works fine for me, sry :)

nenadvkc avatar Dec 29 '21 18:12 nenadvkc