0

I'm pretty new at firebase and in my arduino project, where I use an ESP8266 01S module, I want to hardcode an Id, representing the Arduino project. Then with that Id I want to create if not already created the document in Firestore.

My aproach is using the ESP8266HTTPClient and sending a PATCH request to the url of my project like this

  HTTPClient http;
  String url = "https://firestore.googleapis.com/v1/projects/" FIRESTORE_PROJECT_ID "/databases/(default)/documents/fishtanks/" + tankId + "?key=" + FIREBASE_API_KEY;
  http.begin(client, url);

  String jsonData = "{\"fields\": {"
                      "\"tankId\": {\"stringValue\": \"" + tankId + "\"},"
                      "\"userId\": {\"nullValue\": null},"  // Initialize as null
                      "\"size\": {\"nullValue\": null},"    // Initialize as null
                      "\"watertype\": {\"nullValue\": null}" // Initialize as null
                      "}}";

  httpResponseCode = http.sendRequest("PATCH", jsonData); 

When I run the code and print httpResponseCode, it prints the error -5

The expected result should be (Where TankId=TankId_1)

"fishtanks": {
    "TankId_1": {
      "TankId": "TankId_1",
      "userId": "user_123",
      "size": null,
      "watertype": "freshwater"
    }
}

Edit: I actually was able to generate a document with a custom Id with a PATCH request with Postman using the link https://firestore.googleapis.com/v1/projects/FIRESTORE_PROJECT_ID/databases/(default)/documents/fishtanks/MY_CUSTOM_TANK_ID And having the field values written in the Body of the request like this:

{
  "fields": {
    "TankId": {
      "stringValue": "MY_CUSTOM_TANK_ID"
    },
    "UserId": {
      "nullValue": null
    },
    "TankSize": {
      "nullValue": null
    },
    "WaterType": {
      "nullValue": null
    }
  }
}

But im still not able to get it to work with the ESP8266.

Im using the library ESP8266HTTPClient to make the request and maybe that's messing something up with the firestore API URL being HTTPS.

3
  • 1
    I'm not sure I understand. What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? Commented Aug 25, 2024 at 3:51
  • Edited, thanks for marking the mistake. Commented Aug 25, 2024 at 4:12
  • Where did you get the idea that you can pass a key parameter to the Firestore REST API? The documentation shows completely different ways of authenticating your request, and none of them are as simple as passing a query string parameter. Commented Aug 25, 2024 at 13:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.