WebServer Request processed twice

joihnwargo
Posts: 2
Joined: Sat Aug 26, 2023 4:53 pm

WebServer Request processed twice

Postby joihnwargo » Sat Aug 26, 2023 5:07 pm

I built a web server on an ESP32 device using the Arduino-ESP32 WebServer.h library. The server works great, but I noticed that every request that comes in through the browser processes twice on the server. When I send the same request from Postman, it only processes once. I tried this on a variety of browsers and I get the same result.

I found this article for ESP8266: https://github.com/esp8266/Arduino/issues/6181 that indicates its an issue with Chrome and changing the request type to POST instead of GET should fix the problem. I tried that as well and it didn't help.

Looking at the requests, I updated my Arduino code to write the `server.method()` value to the Serial Monitor and I get the following:

```
WiFi connected
IP address: 192.168.86.230
Web Server running on core 0
Web server: MDNS responder started
LED Management running on core 1
Web server: HTTP server started

Web server: Off
Method: 6
Sent Success response

Web server: Off
Method: 3
Sent Success response
```

So it looks like the web server sees my one request come in with two different request method values.

When I send from Postman the request has a method of 3. What does that mean? I can't find a reference in the code for what server.method() values of 3 and 6 mean.

joihnwargo
Posts: 2
Joined: Sat Aug 26, 2023 4:53 pm

Re: WebServer Request processed twice

Postby joihnwargo » Sun Aug 27, 2023 4:15 pm

I figured it out. The first request is a CORS pre-flight request that CORS uses to validate first that the server can handle the request. The WebServer library only supports GET and PUT requests supposedly but processes that preflight check as well.

I added the following code to my request handlers and the problem went away:

Code: Select all

// Only accept GET requests, this addresses a problem caused by
// CORS preflight check requests that the web server library
// doesn't deal with correctly (OK, not at all)
if (server.method() != HTTP_GET) return;
I wrote about it here: https://johnwargo.com/posts/2023/arduin ... ssor-core/

Who is online

Users browsing this forum: No registered users and 69 guests