Is it possible to use one POST request to add new nodes at different places?

Viewed 22

Is it possible to push a data structure like this to the RTDB without using two POST requests?

{
    "temp": {
        "POST-ID": {
            "value": 30,
            "timestamp": {".sv": "timestamp"}
        }
    },
    "weight": {
        "POST-ID": {
            "value": 123,
            "timestamp": {".sv": "timestamp"}
        }
    }
}

Where the POST-ID would be the unique-id (e.g. "-JSOpn9ZC54A4P4RoqVa") generated with a POST request.

I am basically looking to have a combination of POST and PATCH

PATCH would look something like this:

curl -X PATCH -d '{
  "temp/PUSH-ID": {
            "value": 30,
            "timestamp": {".sv": "timestamp"}
        },
  "weight/PUSH-ID": {
            "value": 123,
            "timestamp": {".sv": "timestamp"}
        }
}' 'https://PROJECT.firebaseio.com/.json'

To create the data I want, I would currently use two POST requests in this style:

curl -X POST -d '{"value": 30, "timestamp": {".sv": "timestamp"}}' 'https://PROJECT.firebaseio.com/temp.json'

Reason

The reason why I would like to have this in one request is to save bandwidth, as the header of the request is currently a substantial part of the whole data used, and not having to transmit that so often would be the goal.

1 Answers

Unfortunately you cannot tell Firebase to generate push IDs in the value that you're passing. In the SDKs as well as in the REST API, you'll have to create your own PUSH-ID values to allow this. Since push keys are pure client-side operations this doesn't add any network overhead, it just adds some code complexity.

Luckily the algorithm for push IDs is pretty well documented in this blog post and the JavaScript is available here. Implementations for other platforms are in the other SDKs, which are open source - so you should be able to dig those up too.

Related