Making WifiClientBasic work

I am trying to get the standard example WifiClientBasic to work. Been at it for hours, nothing seems to convince it to work.

I've opened a new sketch with that example. I have made only 4 changes:

  • I've changed the SSID and password of my router
  • I've changed the address and port of my target web server

Connection works, but the request is refused with a "400 Bad Request" message.

17:38:49.733 -> WiFi connected
17:38:49.733 -> IP address:
17:38:49.733 -> 192.168.0.34
17:38:50.247 -> Connecting to 192.168.0.41
17:38:50.758 -> HTTP/1.1 400 Bad Request
17:38:50.759 -> Closing connection.

The site works : if I hit that address and that port from a web browser, works fine.

I've looked at the apache log to find out if my query looks OK. Nothing wrong, as far as I can tell :

192.168.0.34 - - [01/Jul/2023:17:41:01 +0200] "GET /index.html HTTP/1.1\n" 400 0 "-" "-"
192.168.0.29 - - [01/Jul/2023:17:41:05 +0200] "GET /index.html HTTP/1.1" 200 475 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (K

192.168.0.34 is the arduino, 192.168.0.29 is the ubuntu station I'm running this from. The first query is the one my code creates, the second the one that comes from the web page. Apart from the missing \n, doesn't look too bad.

My setup :

  • Arduino ide 2.1.0
  • DOIT esp32 devkit v1 as a board
  • apache2 running on a RPI. Been working for a (long) while, no problemo.

Any help would be more than welcome.

Cheers,

JP

Hello,
Thanks for the advice. Yeah, got it, apache rejects the request, as indicated in the log file :

192.168.0.34 - - [01/Jul/2023:17:41:01 +0200] "GET /index.html HTTP/1.1\n" 400 0 "-" "-"

I understand the 400 means it rejected the request.

My problem : what is wrong with "GET /index.html HTTP/1.1\n" ???

I've tried adding a CR :

client.print("GET / HTTP/1.1\r\n");

Gets rid of the error message in the Serial Monitor, but it still doesn'nt work, and the apache log still reports the 400 error.

I've tried to retype it, in case I've included an invisible character, but no will do.

Any other ideas ?

Try

client.print("GET / HTTP/1.1\n\n");

"Host" header is mandatory in HTTP 1.1.
lines must end with \r\n (println does it right)
the header of the HTTP request must end with an empty line

btw: we can't see what you send. you didn't show your Arduino sketch

thanks for the tip

YEESSSS
many thanks Jaraj : right you were.
In addition, I've done away with playing with \r and \n. I use print, then println. Let it worry about the ending character.

Here is the working code

client.print("GET /index.html HTTP/1.1");
  client.println();
  client.print("Host: ");
  client.print(host);
  client.println();
  client.println("Connection: close");

  client.println();

thanks so much for the help.

BTW, here is the code in the standard WifiClientExample :

client.print("GET /index.html HTTP/1.1\n\n");

Is there a way to have it corrected, maybe that will save some time for the next guy.

I think the following is easier to the eye.

client.println("GET /index.html HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();

thanks, looks much better.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.