2

I'm trying to send a POST HTTP to a local server running node.js. The GET request works well. The POST using Postman (local) works well, I don't have any firewall. But I can't run it with Arduino.

The code I'm using is a simple SparkFun Client example in which I only changed GET to POST:

void clientDemo() {
  // To use the ESP8266 as a TCP client, use the 
  // ESP8266Client class. First, create an object:
  ESP8266Client client;

  // ESP8266Client connect([server], [port]) is used to 
  // connect to a server (const char * or IPAddress) on
  // a specified port.
  // Returns: 1 on success, 2 on already connected,
  // negative on fail (-1=TIMEOUT, -3=FAIL).
  int retVal = client.connect(destServer, 5000);
  if (retVal == -1) {
    Serial.println(F("Time out"));
    return;
  } else if(retVal == -3) {
    Serial.println(F("Fail connection"));
    return;
  } else if(retVal == 1) {
    Serial.println("Connected with server!");
  }

  // print and write can be used to send data to a connected
  // client connection.
  client.print(httpPost);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available())
    Serial.write(client.read()); // read() gets the FIFO char

  // connected() is a boolean return value - 1 if the 
  // connection is active, 0 if it's closed.
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
}

And the httpPost is:

const String httpPost = "POST /meteo HTTP/1.1\n"
                        "Host: 192.168.0.131:5000\n"
                        "User-Agent: Arduino/1.0\n"
                        "Connection: close\n"
                        "Content-Type: application/x-www-form-urlencoded;\n"
                          "windspeedmph=0&winddir=0&humidity=0&temp=0&pressure=0\n";

All I get in the serial monitor is "Connected with server!"...

What am I doing wrong?

2 Answers 2

1

There needs to be an empty line between the headers and body:

const String httpPost =   "POST /meteo HTTP/1.1\r\n"
                          "Host: 192.168.0.131:5000\r\n"
                          "User-Agent: Arduino/1.0\r\n"
                          "Connection: close\r\n"
                          "Content-Type: application/x-www-form-urlencoded;\r\n"
                          "\r\n"
                          "windspeedmph=0&winddir=0&humidity=0&temp=0&pressure=0\n";

Also by standard you should use \r\n for line breaks and not just \n.
HTTP header line break style

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it does not help. Still only "Connected with server!" and nothing more. I don't even see any POST attempts in logs.
1

In my case it was also important to specify Content-Length field. Then the application started to work as it should. Without the Content-Length field no data was visible on the receiver side. The Content-Length specifies the length of the payload, in your case the length of the string: "windspeedmph=0&winddir=0&humidity=0&temp=0&pressure=0\n".

Comments

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.