Maitham
Maitham

Reputation: 41

Firestore REST API POST request from ESP8266

I'm trying to save a piece of information in the Cloud Firestore from an ESP8266.

I have modified an example code to do so.

I have tried GET request and it was successful and returned the document I stored previously. However when I tried POST request I get the following Error:

{
  "error": {
    "code": 400,
    "message": "Document parent name \"projects/dummy-1cb41/databases/(default)/documents/users\" lacks \"/\" at index 56.",
    "status": "INVALID_ARGUMENT"
  }
}

I think I have not implemented the json payload body correctly. I have many trials but couldn't get it right.

Please help!

Here is the code

/**
   BasicHTTPSClient.ino

    Created on: 20.08.2018

*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};

ESP8266WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("ssid", "password");
}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

//    client->setFingerprint(fingerprint);

    client->setInsecure();

    HTTPClient https;

    String url = "https://firestore.googleapis.com/v1beta1/projects/dummy-1cb41/databases/(default)/documents/users/doc-1";

    Serial.print("[HTTPS] begin...\n");
    if (https.begin(*client, url)) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
//      int httpCode = https.GET();
      https.addHeader("Content-Type", "application/json");
 
      int httpCode = https.POST("{\"fields\":{\"name\":{\"stringValue\":\"a name\"}}}");
      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        } else {
          Serial.println(https.getString());
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }

  Serial.println("Wait 10s before next round...");
  delay(10000);
}

Upvotes: 1

Views: 1144

Answers (1)

Maitham
Maitham

Reputation: 41

I have solved the problem. Thanks to the comments and hints from Luis Manuel and Ben T.

The problem was I made a silly mistake by using the same URL of the GET request for POST request. What That means I was trying to create a document inside a document in the Firestore which is not possible to create a document inside a document. The new document must be created inside a collection. So, after correcting the URL by removing the doc-1 at the end, the new URL will be like this .../users/. It worked perfectly.

Upvotes: 2

Related Questions