Strange response from POST request [SOLVED]

I am setting up an ethernet shield to send values to the php webpage that populates the data in a MySQL database. I already have the setup working with a wifi board and I am porting it to an ethernet board.

I got the data to successfully post to the database, however I then serial read the response from the server, and it says that it has moved and then says that it was unsuccessful with a bad request (posted below). Why is it saying Bad Request when the request actually worked?

My first question is why is this happening. My second question is how to I create a successful response that the arduino can listen for to know that the transmission took place? Are there any examples of this?

Thanks!

Response:

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.0
Date: Fri, 03 Jul 2015 16:46:12 GMT
Content-Type: text/html
Content-Length: 0
Connection: keep-alive
Location: index.php

HTTP/1.1 400 Bad Request
Server: nginx/1.8.0
Date: Fri, 03 Jul 2015 16:46:12 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

400 Bad Request

400 Bad Request


nginx/1.8.0

What code are you using?

Are you accessing a virtual server? Web hosting companies use them. If so, you must insure you send a "Host: mydomain.com" in the request header.

Here's the code:

if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("POST /arduino/add.php HTTP/1.1");
    client.println("Host: www.server.com");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: "); 
    client.println(data.length()); 
    Serial.print("Data Length: ");
    Serial.println(data.length());
    client.println(); 
    client.print(data);
    client.println("Connection: close");
    client.println();

}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
// if there are incoming bytes available
// from the server, read them and print them:
while(client.connected()) {
while(client.available()) {
char ch = client.read();
Serial.write(ch);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}

I am using a shared server on Hostgator.

There is not enough of the code there to help you.

One thing though. The "Connection: close" should be in the header. You have it in the body with the data.

I moved the Connection: close part to the header, then I just get this part as a response:

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.0
Date: Sat, 04 Jul 2015 00:03:20 GMT
Content-Type: text/html
Content-Length: 0
Connection: close
Location: index.php

Insure this is correct. You may get that message if this is not your domain name.

    client.println("Host: www.server.com");

I changed it to that for the post. I use my actual server address.

If that is correct, then you are doing something wrong in your code or the server is not functioning correctly. The server is trying to redirect you to index.php. That is normally what happens if you use the incorrect domain name.

If you used the correct domain name but an incorrect page, it would have returned a 404 error "File not found".

Now I know what it is... I use a "header" call in 'add.php' which causes a 302 redirect.

Thanks for your help.

Now I just have to figure out how to let the arduino know that the server received the info.