Firestore POST request with user ID

Hi all, I’m not able to create a POST request with a custom ID.
I want to create a new document based on the user id that is stored in an app variable.

can someone send me an example how to set it up?

Please provide details describing your issue. Helpful are screenshots about your

  • set-up (data configurator) and
  • example when running a test and
  • the error message you receive

By the way: What helped me a lot to learn about REST API and HTTP-requests is to run tests with postman.com (free to use, no download required).


Most likely the questionmark produces trouble, because this is usally used to add a querry to a request. Lets try leaving it out.

image

Actually I would suggest: …/User/{recordId}

I tried them all…
even without ? or documentID and it’s not working. I always get error 400.
Any other idea? security settings are also allowing read / write.

Even over postman I cannot make a post request with custom ID happen. Is this a limitation of Firestore?

Actually, my first answer was not right:
I looked it up in google and a working way to do it is with the questionmark (as a query).
The description is here.

Checked it in postman and it works with these lines:

POST https://firestore.googleapis.com/v1/projects/{projectID}/databases/(default)/documents/{collectionId}?documentId=12345678

BODY
{fields: {city: {stringValue: “Tokyo”}}}
image

Creates the document in Firebase with the custom Id:
image

1 Like

you are a wizard! thanks!!

Another question along these lines. Is it possible to pass an object without defining the key-value pairs?

So the plan is to pass the object that I receive from “Get current user (Firebase Auth)” directly to Firestore:

aim for Firestore:

It looks like that I have to define each key-value pair within appgyver’s http post formula. Is this correct?
If yes, what is best practise to do this?
My idea is to store all relevant data in app variables and then pass it to Firestore.

Yes, you can declare the appvariable as an object, that matches the schema accordingly. Only one appvariable is required this way.

Thank you!
I have another question regarding the POST method. Since patch is not working I use DELETE and POST in order to get Firestore updated for every data that is created or modified.
I want to reflect changes of the data schema in Firestore. However, this seems to be very challenging.

My current data structure looks like this:

My .json looks like this:

Now, imagine that an user adds a new project or new milestones.
This would require that a new “map” datatype in Firestore is created.

I was trying to store .json objects in a string variable and then appending it to my HTTP request, however this didn’t work out… it also seems like the wrong approach to this problem.

I am using POST in this way:

Any guidance here is much appreciated.

My recomendation is to get this started in smaller steps. It is easier for you to debug, when there are only few “new” parameters.

  • the PATCH method generally works fine for firebase, while PUT is not supported by firebase. It is not necessary to go with DELETE&POST in this matter. If it doesn’t work in your app, you created a bug somewhere (see the recomendation in the first line for easier debugging)
  • regarding your data structure: while you can of course stay with nested mapValues, I wonder if you already considered to use a sub-collection instead. In my taste that would make your data easier to handle.
    Also if you want to avoid sub-collections and you don’t know how many projects a user might create, you might prefer to use arrays.
  • For the request body I personally prefer to work with a data variable, but you can of course use a formula in the “request body” that describes the json-object. As it is not working currently and as it looks quite complex, my recomendation is to test the syntax with less elements and grow it step by step. The free service of postman.com is helpful to build it.

I was somehow under the impression that sub-collections weren’t supported.

You can look up how to adress sub-collections (Firebase) here: Firestore; security rules : if request.auth != null; - #19 by stayfoolish

1 Like