Sarah Sarah - 4 months ago 22
reST (reStructuredText) Question

How can I check if a key exists in my db using Firebase REST API?

I am using the Firebase REST API. I would like to check to see if a key exists in my database before writing to it. If it does not exist, I want to create it and write a set of default data to that key. If it does exist, I want to update the data at that key.

Right now I am attempting this by sending a GET request for that key, as in this example:

curl 'https://docs-examples.firebaseio.com/rest/saving-data/fireblog/posts.json?print=pretty'


and checking the response data. However, while the response data is empty if the key does not exist, it also appears to be empty if there was any other type of error retrieving the data.

Is there another way to check if a key exists using the REST API, like a URI parameter I can use? If not, is there a way to tell if the response data is empty because the key does not exist, or because there was an error?

Answer

The REST API will return a 200 status code and a body of null when the location is empty. Other errors will return a 4xx or 5xx status code.

curl -I https://example.firebaseio.com/does/not/exist.json

HTTP1/1 200 OK
...