HTTP POST sending empty [object Object] and Undefined Values

We have been trying to get a Mega2560 using a WINC1500 WiFi adapter to post data from a capacitive sensor to a Express JS/Node.js server.

We have only been able to get it to post values that are coming through as [object Object] or undefined.

Have tried using JSON.stringify in the server and using application/json as the type. Have tried formatting the data as a JSON string and as plain strings etc etc.

Does anyone have any ideas of what we could be doing wrong? I'll attach the post method and the whole sketch below as well as the example file I have been following.

String postSession(String touchL, String touchR){

    String id = "";
    String postData = "{\n\t\"patientID\": \"3277127\",\n\t\"capacitiveFeedL\": \"1200 1500\",\n\t\"capacitiveFeedR\": \"1200 1500\"\n}";
    //= "{\"patientID\":\"" + cardString + "\",\"capacitiveFeedL\":\"" + touchL + "\",\"capacitiveFeedR\":\"" + touchR + "\"}";

    
    Serial.println("Posting: " + postData);
    delay(100);
    Serial.println(postData.length());
    delay(100);

 if (client.connect(server, 8080)){
  
 
    client.println("POST /api/session HTTP/1.1");
    client.println("Host: 192.168.43.78:8080");
    client.println("Content-Type: text/plain;");
    client.print("Content-Length: ");
    client.println(postData.length() + "\r\n\r\n");
    client.println();
    client.println(postData); 
    delay(1000);
    client.flush();
    client.stop();

    //GET SESSIONID FROM POST RETURN
    return id;
 }
}

Arduino_MySQL_080518.ino (7.05 KB)

PostExample.ino (1.27 KB)

Why does your JSON data have all those \n's and \t's embedded?

It appears that you are forming a valid JSON string, so the problem is on the other end.

I would flush() before delay()

PaulS we had those in there are we have tested the JSON without them and with them just incase the Server was somehow reading tabs and formatting. So we formatted it exactly as the working JSON we have sent via Postman.

I'd be surprised if this is doing what it looks like you expect:

client.println(postData.length() + "\r\n\r\n");

try:

client.print(postData.length() ) ;
client.println( "\r\n\r\n") ;

The documentation for the length() method of the String class is not detailed enough to use to determine what the return type is, but I guess it is not a String.

    client.println(postData.length() + "\r\n\r\n");

client.println();

You're sending 4 CRLF's, there should be only two:

    client.println(postData.length());
    client.println();
client.println(postData);

Why the line ending? You're sending more data than you specified in the Content-Length header.

Your data seems to be JSON, so why do you tell the server to interpret it as plain text?

On top of that:

RFC7230§6.1:
A client that does not support persistent connections MUST send the "close" connection option in every request message.

Read the response from the server to see if it gives an error message.