Arduino client-server post request

Hello everyone,
I am testing a custom API using ESP8266 to send and fetch data from it. Everything looks okay in the code but I always received Bad request error. Here is the code:

#include <ESP8266WiFi.h>

const char* ssid = "iottollgate";
const char* password = "iot2017/2018";

const char* host = "https://transformer-monitoring.herokuapp.com";
const char* path = "POST /api?c=20&l=55&t=87&v=78&n=1 HTTP/1.1\r\nHost: https://transformer-monitoring.herokuapp.com\r\n\r\n";

void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");
}


void loop()
{
  WiFiClient client;

  Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host, 80))
  {
    Serial.println("connected]");

    Serial.println("[Sending a request]");
    client.print(String(path));
    Serial.println("Path:"+String(path));
    
    Serial.println("[Response:]");
    while (client.connected())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\n');
        Serial.println(line);
      }
    }
    client.stop();
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }
  delay(5000);
}

host should be transformer-monitoring.herokuapp.com

https is the protocol and I don't think you can issue a secure http request (over TLS) that way. You would need to either use website's fingerprint, or to use client.setInsecure()

https in the host is the cause of the problem as you pointed out. I removed it and the request was successful. This actually gave me serious headache for days and in fact, I destroyed an ESP32 dev kit in the process.

Thanks very much for this priceless help.

OK if that works it means your server is not HTTPS but just HTTP

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