Go Down

Topic: Getting 400 Bad Request (Read 1 time) previous topic - next topic

jimbooth97

Hi all,

I hope this is the right area to post this but I don't post often, usually read tons of posts and figure stuff out, but this is really wearing me down.

In short the issue is I am trying to connect to my server using GET method and getting Bad Request back.

What is making this unique/odd is that i can connect to other servers (ie other websites I am the webmaster of) but not mine.

I realize the format for the GET request is critical, and i have seen many SLIGHT variations that people say work, but none work for me.

<code>
 Serial.println("connected");
    client.println("GET /temperature.txt HTTP/1.1"); //Send data
    client.println("Host: www.myserver.com");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
</code>

I get in the serial port that i am connected to my wifi, and again, i can access another of my servers by just changing the HOST server, but not my server.

For the record, i am using Network Solutions as the host for my website. I have Read, Write, and Execute for the file permissions.. 

Obviously, I have omitted some of the code for clarity.  I have spent days on this and frankly only today did i try another website just to see if it made any difference, and it did.

Thanks for any ideas,

Jim

jimbooth97

Well, as it seems to happen, once you post a question you find a solution. While this doesn't answer the question why, it does work.

I was using the #include <ESP8266WiFi.h>  library and the GET request would fail and return Bad Request on SOME servers, not all that i tried, but some.. unfortunately the one I needed it to work.

So i tried #include <ESP8266WiFi.h>  and #include <ESP8266HTTPClient.h>  and the example script and that worked perfectly.

So, i hope this helps someone when they run into the bad request, it may not be typos or bad code, it may be you need to include the client library.. and different code..

below worked for me. (if you need all the code, let me know)

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
 
    HTTPClient http;  //Declare an object of class HTTPClient
    String myURL="http://myserver/myfile";
    http.begin(myURL);  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
 
    if (httpCode > 0) { //Check the returning code
 
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
 
    }
 
    http.end();   //Close connection
 
  }

Go Up