I have a nodejs/expressjs backend service and I wish to register my devices using an endpoint. I have to send a POST request to my server with some json encoded data. I have trouble doing this. I can successfully send a GET request and I get response from the server, however when I try to send a POST request I get no response. Here's how I do it:
//Make a post request
void postRequest(const char* url, const char* host, String data){
if(client.connect(host, PORT)){
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
//"Connection: close\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: " + data.length() + "\r\n" +
data + "\n");
//Delay
delay(10);
// Read all the lines of the reply from server and print them to Serial
CONSOLE.println("Response: \n");
while(client.available()){
String line = client.readStringUntil('\r');
CONSOLE.print(line);
}
}else{
CONSOLE.println("Connection to backend failed.");
return;
}
}