Arduino HTTP POST to Firebase

734 Views Asked by At

I have a ESP32 SIM800L module which I would like to send data to a firebase database. I can connect my SIM to GPRS but I am completely stuck on how to send any data via a post request with Arduino. The documents say to use curl for this purpose. So how do I formulate the below code client code to conform to that?

Just a snippet of the library I use

#include <TinyGsmClient.h>
TinyGsmClient client(modem);

const char server[]   = "https://mydatabaseUrl";
const int  port = 80;

And this is my Post request.

Serial.println("start POST request");
      client.print("POST /gps.json HTTP/1.1\r\n");
      client.print(String("Host: ") + server + "\r\n");
      client.println("Connection: close");
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.println(postData.length());
      client.println();
      client.println(postData);

unsigned long timeout = millis();
      while (client.connected() && millis() - timeout < 10000L) {
        // Print available data (HTTP response from server)
        while (client.available()) {
          char c = client.read();
          SerialMon.print(c);
          timeout = millis();
        }
      }
      SerialMon.println();
  client.stop();

This is the output I get

19:30:29.488 -> HTTP/1.1 400 Bad Request
19:30:29.488 -> Content-Length: 54
19:30:29.488 -> Content-Type: text/html; charset=UTF-8
19:30:29.488 -> Date: Fri, 28 May 2021 17:30:29 GMT
19:30:29.488 -> Connection: close
19:30:29.488 -> 
19:30:29.488 -> <html><title>Error 400 (Bad Request)!!1</title></html>
0

There are 0 best solutions below