400 Bad Request after POST

Hi y'all, first of all, I am sorry if this question has been answered before or if this is a really nooby thing to ask. I'm quite new to Arduino and trying to integrate this into a project I am working on.

The problem I am running into is that I want to send a POST request to my web server to store stuff in my database. Sending this post request works perfectly, both from my laptop the development server is currently running on, as with my phone. When trying to do so with the Arduino, I receive a 400 bad request.

Apache logs show:

192.168.0.100 - - [14/Mar/2022:12:37:11 +0100] "POST /api/checkin/684759 HTTP/1.1" 400 325 "-" "Arduino/1.0"
192.168.0.102 - - [14/Mar/2022:12:43:13 +0100] "POST /api/checkin/6 HTTP/1.1" 200 - "-" "PostmanRuntime/7.29.0"

If I'm correct, this indicates data gets returned to the Arduino. The thing is, I have no clue on how to display this data. The code that is currently running is:

char server[] = "192.168.0.102";
IPAddress ip(192, 168, 0, 177);

//sending data
void Send_To_Webserver()
{
  if (client.connect(server, 80)) {
    Serial.println("connected");
    Serial.println("POST api/checkin/684759");

    client.println("POST /api/checkin/684759 HTTP/1.1");
    client.println("Host: 192.168.0.102");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close"); 
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(1000);
  digitalWrite(led_green, LOW);
  digitalWrite(led_red, LOW);
}

If any more information is needed, please let me know. All help is greatly appreciated!

i am not sure, but I hope that the difference of requests can be an answer.

PostmanRuntime/7.29.0 calls /api/checkin/6 and it works
Arduino/1.0 calls /api/checkin/684759 and such file does not exist.

where is the POST's 'body'?

Probably not best practice, but there is no post body. The number at the end of the request is what I need to handle in my functions on the webserver / push to the database.

The request MUST be terminated with and empty line:

client.println("POST /api/checkin/684759 HTTP/1.1");
client.println("Host: 192.168.0.102");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close"); 
client.println(""); //Terminate headers

then do a GET request. it still requires the empty line after the headers

Alright, I'll give it a try. Thank you!

Thank you very much! This did the trick!

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