2

I started to work with HTTPClient and the WiFly library to connect to an SSID and then send POST data to a server. When I was sending the data to my localhost, it was working on localhost, but when changing the URL to a server I am getting this response:

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>openresty</center>
</body>
</html>

My code is the following:

#define HTTP_POST_URL "http://URL/insert.php"
#define HTTP_POST_DATA "181"
SoftwareSerial uart(2, 3);
WiFly wifly(uart);
HTTPClient client;
char get;

while (client.post(HTTP_POST_URL, HTTP_POST_DATA, 10000) < 0) {}

Looking at the constructor of POST in the HTTPClient library it is possible to use POST without defining the header and it will automatically create a default header:

int HTTPClient::post(const char *url, const char *data, int timeout)
{
  return connect(url, "POST", NULL, data, timeout);
}

int HTTPClient::post(const char *url, const char *headers, const char *data, int timeout)
{
  return connect(url, "POST", headers, data, timeout);
}

int HTTPClient::connect(const char *url, const char *method, const char *data, int timeout)
{
  return connect(url, method, NULL, data, timeout);
}

int HTTPClient::connect(const char *url, const char *method, const char *headers, const char *data, int timeout)
{
  char host[HTTP_MAX_HOST_LEN];
  uint16_t port;
  char path[HTTP_MAX_PATH_LEN];
  
  if (parseURL(url, host, sizeof(host), &port, path, sizeof(path)) != 0) {
    DBG("Failed to parse URL.\r\n");
    return -1;
  }
  
  if (!wifly->connect(host, port, timeout)) {
    DBG("Failed to connect.\r\n");
    return -2;
  }
  
  // Send request
  char buf[HTTP_MAX_BUF_LEN];
  snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\n", method, path);
  wifly->send(buf);
  
  // Send all headers
  snprintf(buf, sizeof(buf), "Host: %s\r\nConnection: close\r\n", host);
  wifly->send(buf);
  
  if (data != NULL) {
    snprintf(buf, sizeof(buf), "Content-Length: %d\r\nContent-Type: text/plain\r\n", strlen(data));
    wifly->send(buf);
  }
  
  if (headers != NULL) {
    wifly->send(headers);
  }
  
  // Close headers
  wifly->send("\r\n");
  
  // Send body
  if (data != NULL) {
    wifly->send(data);
  }
  
  return 0;
}

So I am not sure why it worked on localhost but on the hosting.

EDIT:

after logging it to console, this is the request I am sending:

POST /insert_gsr.php HTTP/1.1

Host: www.mywebsite.com
Connection: close

Content-Length: 3
Content-Type: text/plain

EDIT 2:

The example code: https://github.com/Seeed-Studio/WiFi_Shield/blob/master/Examples/wifly_http/wifly_http.ino

is also throwing the same error.

15
  • 1
    When you get a code 400 that means that you are reaching the server, but your request format is not correct. Without providing information about that endpoint we cannot say how the request has to look like. And what do you mean with sending to my localhost? Do you have code on that mimics the actual servers function? Or do you mean that you just get a code 200 with "it works"? Currently I don't see why localhost should be relevant here Commented Jan 17, 2022 at 21:40
  • first I tested out the PHP script where I want to send my POST data on localhost using XAMPP, then I uploaded the same script to my server where it no longer works after I change the URL I am connecting to that of the server. You can see the request I am sending at the bottom of my question. I also used an online API tester tool where I sent the same format of the request as from the Arduino, where it works. Commented Jan 17, 2022 at 21:44
  • Since the code you posted works in 2 out of 3 cases, I would ask about the server code which it doesn’t work with and not the code you posted. I would also perform a test using curl against the script which your code can’t talk to. Commented Jan 17, 2022 at 22:19
  • So with localhost you mean your PC? Have you tested the PHP script on your server (without the Arduino; connecting from your PC to your server)? Did that work? Commented Jan 17, 2022 at 22:23
  • Yes the localhost was an apache server running on my PC on the same WiFi the Arduino connected to. And yes, the script works when calling directly from the browser. My theory is that it worked local, because the request didn´t leave the gateway and the format request gets validated when talking to a remote server. Neverthless, the logged request looks valid to me. Commented Jan 18, 2022 at 8:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.