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.
keyparameter 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.